请写出一个高效的在m*n矩阵中判断目标值是否存在的算法,矩阵具有如下特征:
每一行的数字都从左到右排序
每一行的第一个数字都比上一行最后一个数字大
例如:
对于下面的矩阵:
[ [1, 3, 5, 9], [10, 11, 12, 30], [230, 300, 350, 500] ]要搜索的目标值为3,返回true;
[ [1, 3, 5, 9], [10, 11, 12, 30], [230, 300, 350, 500] ]要搜索的目标值为3,返回true;
[[1,3,5,9],[10,11,12,30],[230, 300, 350, 500]],3
true
if (matrix.length == 1) { return Arrays.binarySearch(matrix[0], target) >= 0; } for (int i = 0; i < matrix.length; i++) { if (target < matrix[i][0]) { // 在上一行 if (i == 0) { return false; } return Arrays.binarySearch(matrix[i - 1], target) >= 0; } else if (target == matrix[i][0]) { return true; } } return false;
public class Solution { /** * * @param matrix int整型二维数组 * @param target int整型 * @return bool布尔型 */ public boolean searchMatrix (int[][] matrix, int target) { // write code here if(matrix == null || matrix.length == 0 || matrix[0].length == 0){return false;} int m = matrix.length, n = matrix[0].length; int l = 0, r = m * n - 1; while(l < r){ int mid = l + (r - l) / 2; int midNum = matrix[mid / n][mid % n]; if(midNum == target){return true;} else if(midNum < target){l = mid + 1;} else{r = mid;} } return matrix[l / n][l % n] == target; } }
public boolean searchMatrix(int[][] matrix, int target) { int m = matrix.length; int n = matrix[0].length; int i = 0, j = n - 1; while (i < m && j >= 0) { if (matrix[i][j] == target) { return true; } else if (matrix[i][j] > target) { j--; } else { i++; } } return false; }
public class Solution { public boolean searchMatrix(int[][] matrix, int target) { if(matrix==null || matrix.length<=0 ) return false; int row = matrix.length; int lie = matrix[0].length; int i; for(i=0;i<row;i++) if(matrix[i][lie-1]>=target) break; if(i>=row) return false; for(int j=0;j<lie;j++) if(matrix[i][j]==target) return true; return false; } }
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0)
return false;
int m = matrix.length;
int n = matrix[0].length;
return find(matrix, 0, m * n - 1, n, target);
}
private boolean find(int[][] matrix, int low, int high, int col, int target) {
while (low <= high) {
int mid = (low + high) / 2;
int x = mid / col;
int y = mid % col;
if (matrix[x][y] == target) {
return true;
} else if (matrix[x][y] > target) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return false;
}
}
/** * Do binary search in this "ordered" matrix */ public boolean searchMatrix(int[][] matrix, int target) { int row_num = matrix.length; int col_num = matrix[0].length; int begin = 0, end = row_num * col_num - 1; while(begin <= end){ int mid = (begin + end) / 2; int mid_value = matrix[mid/col_num][mid%col_num]; if( mid_value == target){ return true; }else if(mid_value < target){ //Should move a bit further, otherwise dead loop. begin = mid+1; }else{ end = mid-1; } } return false; }
public class Solution { public static boolean searchMatrix(int[][] matrix, int target) { int row = 0; boolean isFinded = false; for (int i = 0; i < matrix.length; i ++ ) { if(target == matrix[i][matrix[0].length - 1]) return true; else if(target < matrix[i][matrix[0].length - 1]) { isFinded = true; row = i; break; } } if( ! isFinded) return false; for (int i = 0; i < matrix[0].length; i ++ ) { if(matrix[row][i] > target) break; else if(target == matrix[row][i]) return true; } return false; } }