【LeetCode】【HOT】240. 搜索二维矩阵 II
package hot;
public class Solution240 {
public static void main(String[] args) {
int[][] matrix = {
{1,4,7,11,15},
{2,5,8,12,19},
{3,6,9,16,22},
{10,13,14,17,24},
{18,21,23,26,30}
};
Solution240 solution = new Solution240();
System.out.println(solution.method(matrix, 18));
}
private boolean method(int[][] matrix, int target){
int x = 0;
int y = matrix[0].length - 1;
while(x < matrix.length && y >= 0){
if(matrix[x][y] == target){
return true;
}
if(matrix[x][y] > target){
y--;
}else{
x++;
}
}
return false;
}
}
//时间复杂度为 O(n)
//空间复杂度为 O(1)