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
.
代码如下:
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
List<Integer> list=new ArrayList<>();
for(int i=0;i<matrix.length;i++)
{
for(int j=0;j<matrix[i].length;j++){
list.add(matrix[i][j]);
}
}
if(list.contains(target))
return true; return false;
}
}
240. Search a 2D Matrix II的更多相关文章
- 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 搜索一个二维矩阵 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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- (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
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 二分查找法 依据矩阵的特征非常 ...
- 240. Search a 2D Matrix II&&74. Search a 2D Matrix 都用不严格递增的方法
一句话思路:从左下角开始找.复杂度是o(m+n). 一刷报错: 应该是一个while循环中有几个条件判断语句,而不是每个条件判断语句里去加while,很麻烦 数组越界了.从0开始,y的最大条件应该是& ...
随机推荐
- JAVA输出图形(网上找的)
public class TuXing { public static void main(String[] args) { int i, j, k; for (i = 1; i <= 5; i ...
- wp8.1 C#技巧: Data和ViewModel类编写
在Data.cs namespace PicApp { [DataContract] class DataItem : PropertyChangeNotification { public even ...
- LIS-Program E
最大上升子序列 Description The world financial crisis is quite a subject. Some people are more relaxed whil ...
- 解决python version 2.7 required,which was not find in the registry
程序自动写注册表 http://www.vvivv.com/post-143.html 手工写 http://blog.csdn.net/baikaishui525/article/details/9 ...
- 初识VBS
做了测试快一年了吧,迫于无奈,要学习自动化的只是,首先想到了QTP,但是QTP的脚本是VBS,所以必须要会VBS. VBS其实就是一门计算机编程语言,但是缺少计算机程序语言中的部分要素,对于事件的描述 ...
- iphone判断当前网络连接类型
eachability只能区分出无网络.wifi和wwan(2G&2.5G&3G)类型的网络连接类型,只需重构networkStatusForFlags方法,即可详细区分出2G与3G网 ...
- php的字符串处理
字符串处理: strlen("aaa");取字符串的长度 *** strcmp("aaa","aaa");比较两个字符串,相同的话输出0,不 ...
- HTML--2图片热点,网页划区,拼接
图片热点: 规划出图片上的一个区域,可以做出超链接,直接点击图片区域就可以完成跳转的效果. 示例: 网页划区: 在一个网页里,规划出一个区域用来展示另一个网页的内容. 示例: 网页的拼接: 在一个网络 ...
- 毕向东day01笔记--dos-jdk-jre-环境变量等
1.常用的dos命令,md,rd,dir,c:(进入C盘),del,set classpath 2.JDK和JRE之间的区别: JDK包含JER,JRE包含JVM. 3.环境变量的配置,静态配置--b ...
- Mysql5.0以下 手工注入
order by 20 www. .com/product/introduction.php?id=-65 UNION SELECT user(),2 www. .com/product/introd ...