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. css_样式样式器的分类

    详情:http://www.w3school.com.cn/h.asp 1.标签样式器:此样式器仅对html页面中div标签有效果 div{ background-color: rosybrown; ...

  2. mysqlbinlog快速遍历搜索记录 (转)

    目标,开发人员说有个数据莫名其妙添加了,但是不知道是从哪里添加的,而且应用功能里面不应该添加这样的数据,为了查清楚来源,所 以我就准备去binlog里面找了,但是binlog有好几个月的数,我这样一个 ...

  3. thymeleaf之fragment

    MUEAS项目,web前端采用thymeleaf作为展示层.这个view解析器,个人觉得非常不错.简单而且性能也比较好!个人觉得比JSP和freemarker之类,简单易用! 今天简单记录一下frag ...

  4. ShopEX 4.8.5.81822 前台Getshell

    ShopEX 4.8.5.81822 前台Getshell 作者:unhonker   发布:2014-06-23 00:12   分类:漏洞公布   被撸:8,179次   抢沙发     利用方式 ...

  5. Angular学习(3)- 双向梆定

    示例代码: <!DOCTYPE html> <html ng-app="MyApp"> <head> <title>Study 3& ...

  6. 【maven】之开发pom配置常用插件

    1.打包跳过测试代码 <plugin>  <groupId>org.apache.maven.plugins</groupId>  <artifactId&g ...

  7. foxmail 6.5升级到7.0版本后,旧邮件的导入处理

    随着foxmail 7.0版的火热升级,部分从foxmial 6.5版升级到7.0版的用户可能会出现旧邮件丢失的困扰.这里,foxmail为大家提供的解决方案如下:   打开Foxmail,点击 文件 ...

  8. Mysql触发器简明使用

    触发器:trigger创建触发器的语法mysql> delimiter $mysql> create trigger 触发器名称 after/before(触发时间) insert/upd ...

  9. 限制页面内部链接访问源-HTML注释

        不知道大家有没有碰到过这样一个问题:我修改的是别人的网页,他的超连接是指向一个服务器的某个HTML文件,我把超连接地址修改成了我本地机器的跟首页 同一目录下的一个HTML文件,却发现超连接根本 ...

  10. MVC 介绍

    1>.NUGET,发布软件,管理平台: 2>.Razor,mvc视图引擎,集中生成HTML代码模板@开始,有自己的格式,语法,如同web forms视图引擎web forms view e ...