[LintCode] Search a 2D Matrix
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public boolean searchMatrix(int[][] matrix, int target) { // write your code here if(matrix == null || matrix.length == 0) return false; int i = 0; int j = matrix[0].length-1; while(i < matrix.length && j >=0) { if(target == matrix[i][j]) return true; else if(target < matrix[i][j]) j--; else i++; } return false; } |
Leave A Comment