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.

解法一:暴力解法,遍历二维数组。

代码如下:

public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int rows=matrix.length;
int columns=matrix[0].length;
for(int i=0;i<rows;i++)
for(int j=0;j<columns;j++){
if(target==matrix[i][j])
return true;
}
return false;
}
}

  运行结果:时间复杂度为O(M*N)

方法二:根据矩阵的特点,从右上角元素开始比较,target小,则列数减1,target大,则行数加1.

代码如下:

public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix==null ||matrix.length<1 || matrix[0].length<1){
return false;
}
int col=matrix[0].length-1;
int row=0;
while(col>=0 && row<=matrix.length-1){
if(target==matrix[row][col]){
return true;
}else if(target<matrix[row][col]){
col--;
}else if(target>matrix[row][col]){
row++;
}
}
return false;
}
}

  运行结果:时间复杂度O(M+N)

(medium)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. 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. 使用 Knockout 扩展器扩展 observables

    原文地址:http://knockoutjs.com/documentation/extenders.html 原文名称:Using extenders to augment observables ...

  2. dubbo 解决Multicast java.net.SocketException: No such device

    log4j:WARN No appenders could be found for logger (com.alibaba.dubbo.common.logger.LoggerFactory). l ...

  3. C#连接SQL Server数据库进行简单操作

    环境:VS2010 + SqlServer 2008 首先,按照面向对象的程序设计思想,设计一个数据库操作工具类MyTool.cs,该类中封装了关于数据库连接和操作的方法,各个功能模块在需进行数据库操 ...

  4. ORALCE 游标简单的实例

    --取简单的游标 declare cursor sp is select * from user_tables; myrecord user_tables%ROWTYPE; begin open sp ...

  5. oracle的listener.ora sqlnet.ora tnsnames.ora三个文件的关联性

    学习:http://www.cnblogs.com/william-lee/archive/2010/10/20/1856261.html 之前因为安装的是windows server 2008 r2 ...

  6. Hadoop常见错误解决

    1. 通过命令和查看日志文件查看hadoop启动和运行情况 在NameNode端,可以通过 tail -100 /var/log/hadoop/hadoop/hadoop-hadoop-namenod ...

  7. android操作sdcard中的多媒体文件(一)——音乐列表的制作

    android操作sdcard中的多媒体文件(一)——音乐列表的制作 原文地址 最近做了一个android音乐播放器,个人感觉最难的就是“后台播放”以及有关“播放列表”的部分,但是总算是找到了实现的方 ...

  8. mysql中一对一,一对多,多对多关系

    打个比方吧一个班级有很多学生,他们分别有不同的学号.一个学生对应一个学号,一个学号对应一个学生:这里学生和学号的关系就是一对一.一个班级有很多学生,这个班级只有一个班主任.一个班主任对应多个学生,多个 ...

  9. find_in_set mysql

    有个文章表里面有个type字段,他存储的是文章类型,有 1头条,2推荐,3热点,4图文 .....11,12,13等等 现在有篇文章他既是 头条,又是热点,还是图文, type中以 1,3,4的格式存 ...

  10. Linux下做软RAID

    1.查看有多少块硬盘可用#fdisk -l嗯,一般而言,留下系统盘不动,其它的盘如果大小相同的话,统统合起来做一个raid. 2.决定做什么类型的raid.raid-0.raid-1.raid-5?如 ...