解法一:暴力法

先用一个循环,每次遍历出一个数组nums,然后用sort.SearchInts查出nums数组中target的下标,接着做下判断即可。

//Go
func findNumberIn2DArray(matrix [][]int, target int) bool {
    for _, nums := range matrix {
        //遍历数组切片,查找数组中是否含有target值,如果查找不到,返回值是target应该插入数组的位置(会保持数组的递增顺序)
        i := sort.SearchInts(nums, target) //查找nums数组中target的下标
        //插入的位置小于数组长度 且 插入数组的位置上的值和目标值相等
        if i < len(nums) && target == nums[i] {
            return true
        }
    }
    return false
}

leetcode-cn执行:

执行用时:
28 ms, 在所有 Go 提交中击败了80.58%的用户
内存消耗:6.6 MB, 在所有 Go 提交中击败了70.78%的用户

牛客网运行:

运行时间:7ms
超过8.42%用Go提交的代码
占用内存:3204KB
超过44.21%用Go提交的代码