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

  • Integers in each row are sorted in ascending from left to right.
  • Integers in each column are sorted in ascending from top to bottom.

For example,

Consider the following matrix:

[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]

Given target = 5, return true.

Given target = 20, return false.


解题思路:

从左下角往右上角找,若是小于target就往右找,若是大于target就往上找。时间复杂度O(m+n)  n 为行数,m为列数。

例如找136

但Lintcode上类似题目问题变成找出多少个target,循环中稍微变化下,设置一个count, 就可以了。


Java code:

public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
//check corner case
if(matrix == null || matrix.length == 0) {
return false;
}
if(matrix[0] == null || matrix[0].length == 0) {
return false;
}
//find from bottom left to top right
int n = matrix.length; //row
int m = matrix[0].length; //column
int x = n-1;
int y = 0;while ( x >= 0 && y < m) {
if(matrix[x][y] < target) {
y++;
} else if (matrix[x][y] > target) {
x--;
} else {
return true;
}
}
return false;
}
}

Reference:

1. http://www.jiuzhang.com/solutions/search-a-2d-matrix-ii/

Leetcode 240. Search a 2D Matrix II的更多相关文章

  1. [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 ...

  2. (medium)LeetCode 240.Search a 2D Matrix II

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

  3. Leetcode 240 Search a 2D Matrix II (二分法和分治法解决有序二维数组查找)

    1.问题描写叙述 写一个高效的算法.从一个m×n的整数矩阵中查找出给定的值,矩阵具有例如以下特点: 每一行从左到右递增. 每一列从上到下递增. 2. 方法与思路 2.1 二分查找法 依据矩阵的特征非常 ...

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

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

  5. 【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 ...

  6. 【刷题-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 ...

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

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

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

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

  9. 240 Search a 2D Matrix II 搜索二维矩阵 II

    编写一个高效的算法来搜索 m x n 矩阵中的一个目标值.该矩阵具有以下特性:    每行的元素从左到右升序排列.    每列的元素从上到下升序排列.例如,考虑下面的矩阵:[  [1,   4,  7 ...

随机推荐

  1. pull解析xml文件

    pull解析xml文件 先自己写一个xml文件,存一些天气信息 拿到xml文件 InputStream is = getClassLoader().getResourceAsStream(" ...

  2. 双程动态规划 nyoj61

    题目大意: 在矩阵m*n中,从(1,1)点到(m,n)点,再从(m,n)点到(1,1)点,所走路线经过的同学最大好心值, 要求每个点只能走一遍. 分析: ①我们可以把它只看成两个人同时从(1,1)点, ...

  3. 【JAVA错误笔记】 - Unable add facets project AnnotationWebService CXF 2-x Web Services

    错误描述: 创建webservice接口服务时候提示: Unable add facets project AnnotationWebService CXF 2-x Web Services Unab ...

  4. ACM/ICPC ZOJ1003-Crashing Balloon 解题代码

    #include <iostream> using namespace std; int main() { int **array = new int *[100]; for ( int ...

  5. oracle从各个表取得数据保存到另一个表

    从各个表中取得数据保存另一个表中: CREATE VIEW PARAMETER_view ASWITH tall AS ( SELECT p.PI_NO,--产品序列号 p.SERIALNO,--产品 ...

  6. asp.net 用jquery判断fileupload上传文件的大小和类型和名字

    <script language="javascript" type="text/javascript"> //检查上传文件大小和获取文件名 fun ...

  7. Java教程——CMD手动编译运行失败原因(高手略过)

    (仅对新手,高手略过)在学习Java初期,我们在利用cmd手动编译java程序的时候,会遇到编译成功,但运行却总是提示失败.已经排除了java配置环境的问题,Path和ClassPath以及%JAVA ...

  8. webstorm的默认project编码为系统编码GBK.

    使用新的IDE,而不会设置,会给你带来灾难. 如下为我是用webstorm时遇到的文件编码问题. 纳闷很久,终于发现是IDE的设置问题. 参考

  9. 学习笔记---C++伪函数(函数对象)

    C++里面的伪函数(函数对象)其实就是一个类重载了()运算符,这样类的对象在使用()操作符时,看起来就像一个函数调用一样,这就叫做伪函数. class Hello{ public: void oper ...

  10. 九度OJ 1352 和为S的两个数字

    题目地址:http://ac.jobdu.com/problem.php?pid=1352 题目描述: 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是S,如果有多对数字的和 ...