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的更多相关文章

  1. leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II

    74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...

  2. 【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 ...

  3. 【刷题-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 ...

  4. [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 ...

  5. 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. (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 ...

  7. 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 ...

  8. Leetcode 240 Search a 2D Matrix II (二分法和分治法解决有序二维数组查找)

    1.问题描写叙述 写一个高效的算法.从一个m×n的整数矩阵中查找出给定的值,矩阵具有例如以下特点: 每一行从左到右递增. 每一列从上到下递增. 2. 方法与思路 2.1 二分查找法 依据矩阵的特征非常 ...

  9. 240. Search a 2D Matrix II&&74. Search a 2D Matrix 都用不严格递增的方法

    一句话思路:从左下角开始找.复杂度是o(m+n). 一刷报错: 应该是一个while循环中有几个条件判断语句,而不是每个条件判断语句里去加while,很麻烦 数组越界了.从0开始,y的最大条件应该是& ...

随机推荐

  1. bzoj 1997: [Hnoi2010]Planar

    #include<cstdio> #include<cstring> #include<iostream> #define M 20005 #define N 20 ...

  2. Countries in War (POJ 3114) Tarjan缩点+最短路

    题目大意: 在一个有向图中,每两点间通信需要一定的时间,但同一个强连通分量里传递信息不用时间,给两点u,v求他们最小的通信时间.   解题过程: 1.首先把强连通分量缩点,然后遍历每一条边来更新两个强 ...

  3. 戴文的Linux内核专题:02源代码

    转自Linux中国 在下载并解压内核源代码后,用户可以看到许多文件夹和文件.尝试去找一个特定的文件或许是一个挑战.谢天谢地,源代码以一个特定的方式组织的.这使开发者能够轻松找到任何文件或者内核的一部分 ...

  4. 免费获得NOD32 半年、1年 激活码-14.08.12到期

    地址: http://nod32.ruanmei.com/ 活动时间: 2014年8月6日 - 8月12日(全部送完将提前终止). 活动规则: 1.每台电脑限领1枚NOD32激活码: 2.领到的NOD ...

  5. IOS 中的KVO模式 观察者模式

    / // KvoObject.h // KVO // // Created by lin kang on 16/6/7. // Copyright © 2016年 lin kang. All righ ...

  6. 2014年3月份第2周51Aspx源码发布详情

    MVC+EF某钢电子交易平台源码  2014-3-10 [VS2012]功能介绍:本源码是一套完整的电子交易平台系统,完全基于ASP.NET MVC+EF三层构架,开发环境为Visual Studio ...

  7. color 的一些处理

    1.UIImage转换成UIcolor cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@" ...

  8. allegro si(三)

    前言:si的教程市面上是很少的,layout是台湾工程师的强项,还有就是日本人,国人爱用AD. si的教程中靠谱的还是张飞的收费课程,还有华为的资料. Cadence SI 仿真实验步骤如下: 1.熟 ...

  9. Design Patterns---- Strategy 模式

    设计模式:可复用面向对象软件的基础 书中对 Strategy 模式的定义如下: 定义了一系列的算法,把它们一个个封装起来,并且使它们可相互替换.本模式使得算法可独立于它的用户而变化. 案例:设计一个商 ...

  10. false等于0???

    看到一个函数strpos($string,$str),用于在字符串$string中查找$str,如果在$string中查找到$str,则返回第一次出现的位置,起始位置为0:如果$string中不包含$ ...