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. 设置AFNetworking网络请求的超时时间

    http://www.mamicode.com/info-detail-511816.html // 设置超时时间 [manager.requestSerializer willChangeValue ...

  2. android自定义控件实例(Linearlayout组合TextView和ImageView)

    2013-12-18 11:25:22 转载自: http://www.open-open.com/lib/view/open1328836804515.html 很多时候android常用的控件不能 ...

  3. 移动设备和SharePoint 2013 - 第3部分:推送通知

    博客地址:http://blog.csdn.net/foxdave 原文地址 在该系列文章中,作者展示了SharePoint 2013最显著的新功能概观--对移动设备的支持. 该系列文章: 移动设备和 ...

  4. SharePoint 跨域还原网站一则

    博客地址:http://blog.csdn.net/foxdave 源端:执行PowerShell命令备份网站集 Backup-SPSite http://server_name/sites/site ...

  5. ODI 12c 安装

    软件下载地址: http://www.oracle.com/technetwork/middleware/data-integrator/downloads/index.html 下载这个版本: Or ...

  6. Event Handling on Mac

    Keyboard/Mouse Event + Cocoa AppleEvent + Cocoa AppleEvent + CommandLine App(w/o UI) + CoreFoundatio ...

  7. Design Patterns

    经典的<设计模式>一书归纳出23种设计模式,本文按<易学设计模式>一书归纳分类如下:1.创建型模式 前面讲过,社会化的分工越来越细,自然在软件设计方面也是如此,因此对象的创建和 ...

  8. 2016-1-7第一个完整APP 私人通讯录的实现 6:在联系人界面增加删除联系人的功能

    一:在viewDidLoad方法中代码添加一个UIBarButtonItem,并将其的类型设置成垃圾桶,代码如下: - (void)viewDidLoad { [super viewDidLoad]; ...

  9. 2016-1-3点菜系统demo的实现,pickerView的学习

    // // ViewController.m // pickView // // Created by Mac on 16/1/3. // Copyright © 2016年 Mac. All rig ...

  10. C#操作txt问件,进行清空添加操作

    //把txt清空 FileStream stream = File.Open(Adr,FileMode.OpenOrCreate,FileAccess.Write); stream.Seek(, Se ...