一句话思路:从左下角开始找。复杂度是o(m+n)。

一刷报错:

  1. 应该是一个while循环中有几个条件判断语句,而不是每个条件判断语句里去加while,很麻烦
  2. 数组越界了。从0开始,y的最大条件应该是<n,不是《n
  3. 风格上:++ -- 都是一个数字的一元运算,不用加空格
  4. 超时了。循环体结构:
        while (x >= 0 && y <= n) {
if (matrix[x][y] == target) {
count ++;
}
else if (matrix[x][y] > target) {
x --;
}
else if(matrix[x][y] < target) {
y ++;
}
else {
x --;
y ++;
}
}

错误的原因在于:没有意识到这个矩阵可以是不严格单调递增的,在最左一列、最下一行找到后,继续朝右上方找,还有可能找到。

一句话总结:注意不严格单调递增的特点,在边界线上找到后还要继续找。而且还要注意不要数组越界。(有0时写小于号)

class Solution {
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;
int x = m - 1;
int y = 0;
int count = 0; while (x >= 0 && y < n) {
if (matrix[x][y] > target) {
x--;
}
else if(matrix[x][y] < target) {
y++;
}
else {
count++;
x--;
y++;
}
} //if (count > 0) {
// return true;
//}
return count;
}
}

240. Search a 2D Matrix II&&74. Search a 2D Matrix 都用不严格递增的方法的更多相关文章

  1. Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)

    Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...

  2. LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II)

    题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 m ...

  3. leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II

    74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...

  4. 【leetcode】74. Search a 2D Matrix & 240. Search a 2D Matrix II

    题目如下:这两个题目可以用同样的代码来解答,因此就合并在一起了. 题目一: 题目二: 解题思路:两个题目的唯一区别在于第二个题目下一行的最小值不一定会小于前一行的最大值.但是不管怎么样我们可以确定的是 ...

  5. LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37

    240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...

  6. 240.Search in a 2D Matrix II

    /* * 240.Search in a 2D Matrix II * 2016-6-17by Mingyang * From left-bottom to right-top * 他这道题目虽说是用 ...

  7. 【LeetCode】240. Search a 2D Matrix II

    Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...

  8. [LeetCode] 240. Search a 2D Matrix II 搜索一个二维矩阵 II

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  9. 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. Linux设置默认shell脚本效果

    效果如图: 实现方法:在当前用户的家目录下新建文件.vimrc [root@nodchen-db01-test ~]# pwd/root [root@nodchen-db01-test ~]# fil ...

  2. 关于Linux DNS部分处理

    如果不能ping 通外网地址进行解析的话进行如下处理

  3. 1038 Recover the Smallest Number (30 分)

    1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...

  4. golang web框架 beego 学习 (一) 环境搭建

    下面的命令我都是在$GOPATH的路径下执行的: 1. 首先下载beego框架: go get github.com/astaxie/beego (注意:运行上面命令时没有反应,需要在etc/host ...

  5. 从线性回归到CNN【转】

    原地址:http://zhangliliang.com/2014/06/14/from-lr-to-cnn/ csdn:    http://blog.csdn.net/t0903/article/d ...

  6. spark-submit 提交Application

    在spark1.0中推出spark-submit来统一提交applicaiton ./bin/spark-submit \ --class <main-class> --master &l ...

  7. sorted()&enumerate()

    d = {1:2,3:1,44:5,4:5,7:8}l = d.items() #转换为列表print(l)  # dict_items([(1, 2), (3, 1), (44, 5), (4, 5 ...

  8. node和yarn

    nvm 版本管理工具 https://github.com/coreybutler/nvm-windows/releases   nvm-setup   nvm install +版本号   加版本 ...

  9. nodejs——压缩文件_archiver

    工作需要,由于html无法访问并下载带有中文的路径,例子:“127.0.0.1::8088/files/第一张图片.jpg”,所以想到了先将原图片压缩并命名为不带中文的文件名,下载后用户自行解压缩的方 ...

  10. 单例模式(Singleton)

    单例模式  Singletonn Pattern Ensure a class has only one instance, and provide  a global point of access ...