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 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的更多相关文章
- [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 ...
- (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 ...
- Leetcode 240 Search a 2D Matrix II (二分法和分治法解决有序二维数组查找)
1.问题描写叙述 写一个高效的算法.从一个m×n的整数矩阵中查找出给定的值,矩阵具有例如以下特点: 每一行从左到右递增. 每一列从上到下递增. 2. 方法与思路 2.1 二分查找法 依据矩阵的特征非常 ...
- leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II
74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...
- 【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 ...
- 【刷题-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 ...
- 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】74. Search a 2D Matrix & 240. Search a 2D Matrix II
题目如下:这两个题目可以用同样的代码来解答,因此就合并在一起了. 题目一: 题目二: 解题思路:两个题目的唯一区别在于第二个题目下一行的最小值不一定会小于前一行的最大值.但是不管怎么样我们可以确定的是 ...
- 240 Search a 2D Matrix II 搜索二维矩阵 II
编写一个高效的算法来搜索 m x n 矩阵中的一个目标值.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列.例如,考虑下面的矩阵:[ [1, 4, 7 ...
随机推荐
- 那天有个小孩跟我说LINQ(七)转载
1 LINQ TO XML(代码下载) 准备:新建项目 linq_Ch7控制台程序,新建一个XML文件夹,我们就轻松地学习一下吧 XDocument ...
- swift入门-day02
1.函数 2.闭包 3.构造函数基础 4.重载构造函数 5.KVC构造函数 6.遍历构造函数 7.懒加载 8.只读属性 1.函数 掌握函数的定义 掌握外部参数的用处 掌握无返回类型的三种函数定义方式 ...
- [转]:移动端H5页面高清多屏适配方案
原文链接:http://www.tuicool.com/articles/YJviea 背景 开发移动端H5页面 面对不同分辨率的手机 面对不同屏幕尺寸的手机 视觉稿 在前端开发之前,视觉MM会给我们 ...
- jeesite 一对多,对子表单独修改时出现 HV000030: No validator could be found for type:
这是由于springmvc校验功能造成的,解决办法参见我的百度回答 http://zhidao.baidu.com/question/2141673418419314468
- Java实战之01Struts2-03属性封装、类型转换、数据验证
九.封装请求正文到对象中 1.静态参数封装 在struts.xml配置文件中,给动作类注入值.调用的是setter方法. 原因:是由一个staticParams的拦截器完成注入的. 2.动态参数封装: ...
- 解决Mysql的主从数据库没有同步的两种方法
今天发现Mysql的主从数据库没有同步 先上Master库: mysql>show processlist; 查看下进程是否Sleep太多.发现很正常.show master status; ...
- Qt标准对话框之QColorDialog
Qt中提供了一些标准的对话框,用于实现一些常用的预定义功能,比如本节中将要介绍的颜色对话框——QColorDialog. 在不同的系统平台下,颜色对话框的显示效果可能会有所不同,主要因系统主题风格而异 ...
- c++与C# winform的消息通讯--(结构体与byte数组的使用)
近期正在做一个蓝牙驱动的使用程序,其中有一块从c++发送数据到C#的部分,网上查了很多资料,大多都是介绍如何通过调用函数获取用户数据.并且在消息发送中,很少介绍如何发送一个结构体,并且结构体里面有 b ...
- [转载]5分钟了解Mockito
原文链接: http://liuzhijun.iteye.com/blog/1512780/ 5分钟了解Mockito 博客分类: Open SourceJava 一.什么是mock测试,什么是moc ...
- php中CURL技术模拟登陆抓取数据实战,抓取某校教务处学生成绩。
这两天有基友要php中curl抓取教务处成绩的源码,用于微信公众平台的开发.下面笔者只好忍痛割爱了.php中CURL技术模拟登陆抓取数据实战,抓取沈阳工学院教务处学生成绩. 首先,教务处登录需要验证码 ...