https://leetcode.cn/problems/er-wei-shu-zu-zhong-de-cha-zhao-lcof/
func findNumberIn2DArray(matrix [][]int, target int) bool { return find2(matrix,target) } //分别用两个指针,按照行和列的维度,从左下角开始线性搜索 //时间复杂度为O(n+m),空间复杂度为O(1) func find(nums [][]int,target int) bool{ if len(matrix)==0{ return false } rows:=len(nums) cols:=len(nums[0]) col:=0 row:=rows-1 //从左下角(row,0)的数开始找 for row>=0&&col<cols{ if nums[row][col]>target{ row-- //行减 }else if nums[row][col]==target{ return true }else{ col++ //列加 } } return false } //二维数组降成一维数组,然后遍历一遍找target值 func find2(matrix [][]int,target int) bool{ nums:=make([]int,0) for i:=0;i<len(matrix);i++{ nums=append(nums,matrix[i]...) } //fmt.Println(nums) for i:=range nums{ if nums[i]==target{ return true } } return false }
原创文章,作者:306829225,如若转载,请注明出处:https://blog.ytso.com/270834.html