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 ...
随机推荐
- Linux中tshark(wireshark)抓包工具使用方法详解
在Linux下,当我们需要抓取网络数据包分析时,通常是使用tcpdump抓取网络raw数据包存到一个文件,然后下载到本地使用wireshark界面网络分析工具进行网络包分析.最近才发现,原来wires ...
- 【转】浅析Windows编程的剪贴板
摘要: 本文对Windows剪贴板机制作了深入.全面的阐述,具体内容包括:文本.位图.DSP.自定义格式剪贴板的使用和多数据项和延迟提交技术. 关键词: VC++6.0: 剪贴板机制:数据格式:延迟提 ...
- ZeroMemory
ZeroMemory: 用0填充一个内存块 void ZeroMemory( [in] PVOID Destination, //内存块开始地址 [in] SIZE_T Length //填充块大小 ...
- 关于block块使用的6点注意事项
对于刚学习OC新伙伴,block块一直都是一个比较纠结.比较难懂的知识点,不过,在使用一段时间后,就会感觉很酸爽.block块的原理及使用我就不再赘述,网上有很多这方面的资料.我个人使用这么长时间以来 ...
- Mac 下显示隐藏文件
将下面的命令粘贴进终端,按提示操作即可(可能需要输入电脑密码) 显示:defaults write com.apple.finder AppleShowAllFiles -bool true 隐藏:d ...
- c#安全性、稳定性、高性能需要注重的点
1.弱事件 2.类型安全(包括类型转换时可能的异常) 3.线程安全 5.装箱和拆箱 6.闭包传递局部变量时可能遇到的问题. 7.用于dictionary的键,要同时实现GetHashCode和Equa ...
- Spring学习之代理
Spring的核心就是IOC和AOP IOC就是控制反转: 就是用配置文件的方式给javabean 赋值. 正常的在程序里;用new 的方式创建一个对象的时候,他就固定了值, 如果是注入的方式的话 ...
- Codevs 4560 NOIP2015 D2T2 子串
> 4560 NOIP2015 D2T2 子串 时间限制: 1 s 空间限制: 128000 KB 题目等级:黄金 Gold 题目描述 Description 有两个仅包含小写英文字母的字符串A ...
- spark - 将RDD保存到RMDB(MYSQL)数据库中
SCALA连接数据库批量插入: scala> import java.sql.DriverManager scala> var url = "jdbc:mysql://local ...
- emment语法
emment插件语法.md 元素 div → <div></div>foo → <foo></foo> 嵌套运算符 子: > div>ul& ...