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. Android开发之ViewPager

    什么是ViewPager? ViewPager是安卓3.0之后提供的新特性,继承自ViewGroup,专门用以实现左右滑动切换View的效果. 如果想向下兼容就必须要android-support-v ...

  2. Solr4.7新建core

    Solr里面的core就像数据库里面的一个表,用来管理索引和相关配置. 一. 使用示例core 下载的solr完整包里面solr-4.7.0\example\multicore这个文件夹下面有2个示例 ...

  3. IOS 高级开发 runtime(二)

    二.移魂大法 使用runtime还可以交换两个函数.先贴上代码和执行结果. #import <Foundation/Foundation.h> @interface DZLPerson : ...

  4. Java中多线程的使用!!

    简介:       1.要了解多线程,首先我们得先了解进程和线程.那么什么是进程?进程就是一个正在运行的程序分配内存让应用程序能够运行的叫做进程.那么什么又是线程呢?线程:在一个程序中,负责代码的执行 ...

  5. libsdl中文输入法候选列表不显示解决方案

    libsdl输入中文时输入法不显示,将WM_IME_SETCONTEXT事件处理给注释即可. WM_IME_SETCONTEXT If the application draws the compos ...

  6. shell中的条件判断

    read命令 命令的语法: read [参数] 变量名 常用的参数如下: -t timeout 设定超时时间. -p prompt 设定提示信息,该提示信息将会显在光标前. 条件测试: 条件测试可以判 ...

  7. 设置用户sudo -s拥有root权限

    开通普通用户的ROOT权限,上线了可以禁止用户使用root权限 修改配置文件 vi etc/sudoers 在 root    ALL=(ALL) ALL那么你就在下边再加一条配置:hjd ALL=( ...

  8. MySQL - “Timeout error occurred trying to start MySQL Daemon”解决方法

    前几天,网站出现Many connections的问题,进入mysql,show full processlist发现有154个进程,晕....直接service mysqld restart 但是不 ...

  9. Config IIS server6.0-- HTTP 错误 500.21 - Internal Server Error 解决方案

    HTTP 错误 500.21 - Internal Server Error 解决方案         不久前重新安装了Windows7,在安装了VS2010 开发平台之后,将网站发布到IIS,访问发 ...

  10. lua在MacOS系统上的安装方法

    lua是一种非常小巧的脚本语言,由标准C编写而成,可以很方便的调用c/c++或者被c/c++.另外相关的还有一个luaJIT,是lua在某些平台上的编译器. 我们在这里只安装lua. 1.检测电脑上是 ...