📜  在二维矩阵中搜索 - Java 代码示例

📅  最后修改于: 2022-03-11 14:52:21.843000             🧑  作者: Mango

代码示例1
// Search a 2D Matrix II
// https://leetcode.com/problems/search-a-2d-matrix-ii
class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        int row = 0;
        int col = matrix[0].length-1;
        
        while (row < matrix.length && col >= 0){
            if (matrix[row][col] == target) return true;
            if (matrix[row][col] < target) row++;
            else if (matrix[row][col] > target) col--;
        }
       return false;
    }
}