java数据结构与算法刷题目录(剑指Offer、LeetCode、ACM)-----主目录-----持续更新(进不去说明我没写完):https://blog.csdn.net/grd_java/article/details/123063846 |
---|

- 法一:把整个数组遍历一遍,时间复杂度O(m*n)
- 法二:每一行用二分搜索,那么时间复杂度就是O(m *
l
o
g
2
n
log_2{n}
log2n)
- 法三:利用题目条件,每一行都是升序序列,每一列也是升序序列。所以我们从左下角判断,每次剔除一行或一列,最终尝试找到目标数。时间复杂度O(m+n)





class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int m = matrix.length,n = matrix[0].length;//获取行和列的大小
int r = m-1,c = 0;//r表示当前第几行,c表示的当前第几列。初始指向左下角
while(r>=0 && c//只要下标合理,就继续判断
int bottomLeft = matrix[r][c];//获取左下角的元素
if(bottomLeft == target) return true;//如果当前元素就是要找的,直接返回
else if(bottomLeft>target) r--;//如果左下角元素比target大,说明这一行都大于目标值,判断它的上一行
else if(bottomLeft