You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
lavos/cap3/ex3.go

29 lines
897 B

package cap3
// Ex3 求鞍点数,行上最小,列上最大
func Ex3(matrix [][]int) (int, int, int) {
rowMinIdx := map[int]int{} // key: 行索引, value: 最小值所在列
colMaxIdx := map[int]int{} // key: 列索引, value: 最大值所在行
for rowIdx, row := range matrix {
rowMinIdx[rowIdx] = 0
for colIdx, currNum := range row {
if currNum < row[rowMinIdx[rowIdx]] {
rowMinIdx[rowIdx] = colIdx
}
if _, has := colMaxIdx[colIdx]; !has {
colMaxIdx[colIdx] = rowIdx
} else if currNum > matrix[colMaxIdx[colIdx]][colIdx] {
colMaxIdx[colIdx] = rowIdx
}
}
}
for rowIdx, colIdx := range rowMinIdx {
if rowIdx == colMaxIdx[colIdx] {
return rowIdx, colIdx, matrix[rowIdx][colIdx]
}
}
return -1, -1, 0
}