public class Solution { /** * O(m + n) time complexity */ public int searchMatrix(int[][] matrix, int target) { if(matrix == null || matrix.length == 0) return 0; if(matrix[0] == null || matrix[0].length == 0) return 0; int m = matrix.length; int n = matrix[0].length; //Every time observe the point in the upper right corner int cur_i = 0; int cur_j = n - 1; int sum = 0; while(cur_i<m && cur_j >= 0){ if(matrix[cur_i][cur_j] == target){ sum ++; cur_j --; //This column can no longer satisfy the condition }else if(matrix[cur_i][cur_j] > target){ cur_j --; //This column can no longer satisfy the condition }else{ cur_i ++;//The point is smaller than the target point, indicating that the line cannot meet the conditions } } return sum; } }
[LintCode] 38. Search a 2D Matrix_II
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=325506685&siteId=291194637
Recommended
Ranking