Search a 2D Matrix II
给一个排序的2d的数组, 搜索元素, 二分查找的2d版.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class Solution { public boolean searchMatrix(int[][] matrix, int target) { if(matrix == null || matrix.length == 0) return false; int i = 0; int j = matrix[0].length-1; while(i < matrix.length && j >=0) { if(matrix[i][j] > target) j--; else if(matrix[i][j] < target) i++; else return true; } return false; } } |