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. js的严谨模式

    一.怎么用 <script type="text/javascript"> "use strict";     //放在脚本文件第一行,整个脚本将以 ...

  2. PowerMock 遇到的问题——2

    如果我们要测试的类继承另一个类,而且在构造方法中有super():那么怎么Mock那个super语句呢? 在PowerMock中有一个suppressConstructor方法,具体写法如下: Pow ...

  3. struts中的请求数据自动封装

    Struts 2框架会将表单的参数以同名的方式设置给对应Action的属性中.该工作主要是由Parameters拦截器做的.而该拦截器中已经自动的实现了String到基本数据类型之间的转换工作.在st ...

  4. 标准库源码--wsgi服务器

    功能模块化带来可自由组装的便利: 使用python的mixin特性装配 class ThreadHTTPServer(ThreadingMixin, HTTPServer): pass 上面表示1个请 ...

  5. 经典线程同步 互斥量Mutex

    阅读本篇之前推荐阅读以下姊妹篇: <秒杀多线程第四篇一个经典的多线程同步问题> <秒杀多线程第五篇经典线程同步关键段CS> <秒杀多线程第六篇经典线程同步事件Event& ...

  6. 传Windows 9预览版今秋发布

    据ZDNet长期关注微软动态的资深人士玛丽•乔•弗利(Mary Jo Foley)称,Windows 9预览版将会在9月或者10月推出.按照这一进度,代号为“Threshold’”的Windows 9 ...

  7. SharePoint 2013 开发——概述

     博客地址:http://blog.csdn.net/FoxDave 近来阅读SharePoint 2013开发一书,带着与大家一起分享其中的内容. 部署场景: 本地部署(On-Premise D ...

  8. ios 8+ (xcode 6.0 +)应用程序Ad Hoc 发布前多设备测试流程详解

    我们开发的程序在经过simulator以及自己的iOS设备测试后,也基本完成应用程序了,这时候我们就可以把它发布出去了更更多的人去测试,我们可以在iOS平台使用ad hoc实现. 你在苹果购买的开发者 ...

  9. MapReduce实现TopK的示例

    由于开始学习MapReduce编程已经有一段时间了,作为一个从编程中寻找自信和乐趣以及热爱编程的孩子来讲,手开始变得很“痒”了,很想小试一下身手.于是自己编写了TopK的代码.TopK的意思就是从原文 ...

  10. JS原生回到顶部效果

    // 回到顶部 onload = function () { var oBtnTop = document.getElementById('toTop'); var timer = null; oBt ...