1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// @Title: 搜索二维矩阵 II (Search a 2D Matrix II)
// @Author: 15816537946@163.com
// @Date: 2020-06-25 07:15:36
// @Runtime: 32 ms
// @Memory: 6.3 MB
func searchMatrix(matrix [][]int, target int) bool {
    if len(matrix) == 0 {
        return false
    }

    i := len(matrix)-1
    j := 0
    for {
        if i<0 || j > len(matrix[0])-1 {
            return false
        }
        if target == matrix[i][j] {
            return true
        } else if target > matrix[i][j] {
            j++
        } else {
            i--
        }
    }
    return false
}