题解 | #在行列都排好序的矩阵中找指定的数#
在行列都排好序的矩阵中找指定的数
http://www.nowcoder.com/practice/b929be9dbbaa489a91afa3fec195c228
go语言实现,再练习一下go的标准输入
import (
"fmt"
)
func main() {
var (
N int
M int
K int
n int
)
fmt.Scan(&N,&M,&K)
nums := make([][]int,N)
for i:=0;i<N;i++ {
nums[i] = make([]int,M)
for j:=0;j<M;j++ {
fmt.Scan(&n)
nums[i][j] = n
}
}
result := find(nums,K)
fmt.Println(result)
}
func find(nums [][]int,target int) string {
rows,cols := len(nums), len(nums[0])
row, col := 0,cols-1
for row < rows && col >= 0 {
if nums[row][col] == target {
return "Yes"
} else if nums[row][col] > target {
col --
} else {
row++
}
}
return "No"
}