public class Solution {
/**
* @param matrix: A list of lists of integers
* @param: A number you want to search in the matrix
* @return: An integer indicate the occurrence of target in the given matrix
*/
public int searchMatrix(int[][] matrix, int target) {
// write your code here
if (matrix == null) return 0;
int row = matrix.length;
if (row == 0) return 0;
int column = matrix[0].length;
int currentRow = row - 1;
int currentColumn = 0;
int result = 0;
while (currentRow >= 0 && currentColumn <= column - 1) {
if (matrix[currentRow][currentColumn] == target) {
result++;
currentRow--;
} else if (matrix[currentRow][currentColumn] < target) {
currentColumn++;
} else {
currentRow--;
}
}
return result;
}
}
38.Search a 2D Matrix II (0(M+N))
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 典型老题,从右上角开始搜索,往下是增往左是减。比如.[[1 4],[2 5]]右上角元素即4 ir是行标,ic是列...
- 题目来源Write an efficient algorithm that searches for a valu...