Difficulty:medium

 More:【目录】LeetCode Java实现

Description

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 from left to right.
  • The first integer of each row is greater than the last integer of the previous row.

Example 1:

Input:
matrix = [
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
target = 3
Output: true

Example 2:

Input:
matrix = [
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
target = 13
Output: false

Intuition

regard the matrix as an array, and then use binary search.

  matrix[x][y]=array[x*cols+y]

  array[m]=matrix[m/cols][m%cols]

Solution

    public boolean searchMatrix(int[][] matrix, int target) {
if(matrix==null || matrix.length<=0 || matrix[0].length<=0)
return false;
int rows=matrix.length; //行数
int cols=matrix[0].length; //列数
int low=0;
int high=rows*cols-1;
while(low<=high){
int mid=(low+high)/2;
if(matrix[mid/cols][mid%cols]==target){
return true;
}else if(matrix[mid/cols][mid%cols]>target){
high=mid-1;
}else if(matrix[mid/cols][mid%cols]<target){
low=mid+1;
}
}
return false;
}

  

Complexity

Time complexity : O(log(n*m))

Space complexity : O(1)

What I've learned

1. Ought to have a good command of the change between array <=> matrix, especially the change of their indexes

 More:【目录】LeetCode Java实现

【LeetCode】74. Search a 2D Matrix的更多相关文章

  1. 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...

  2. 【leetcode】74. Search a 2D Matrix & 240. Search a 2D Matrix II

    题目如下:这两个题目可以用同样的代码来解答,因此就合并在一起了. 题目一: 题目二: 解题思路:两个题目的唯一区别在于第二个题目下一行的最小值不一定会小于前一行的最大值.但是不管怎么样我们可以确定的是 ...

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

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

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

  5. 【一天一道LeetCode】#74. Search a 2D Matrix

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...

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

  7. LeetCode OJ 74. Search a 2D Matrix

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  8. 【Lintcode】038.Search a 2D Matrix II

    题目: Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence ...

  9. 【Lintcode】028.Search a 2D Matrix

    题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...

随机推荐

  1. linux(1):VMware虚拟软件下安装centos6.8

    前言:Linux是一种自由和开放源代码的类UNIX操作系统,继承了Unix以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统.本人学习Linux已经有一段时间了,从一开始的小白到现在的略有所悟 ...

  2. LeetCode 6罗马数字转整数

    罗马数字包含以下七种字符:I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并列 ...

  3. php Mockery 错误 "call_user_func_array() expects parameter 1 to be a valid callback, class 'Mockery\Expectation' does not have a method"

    错误写法 $mock = Mockery::mock(MyClass::class) ->shouldReceive('foo') ->once() ->with($arg) -&g ...

  4. linux读写锁

    一.概述                                                    读写锁与互斥量的功能类似,对临界区的共享资源进行保护!互斥量一次只让一个线程进入临界区, ...

  5. 何凯文每日一句||DAY10

  6. 何凯文每日一句打卡||DAY5

  7. Spring Cloud (十五)Stream 入门、主要概念与自定义消息发送与接收

    前言 不写随笔的日子仿佛就是什么都没有产出一般--上节说到要学Spring Cloud Bus,这里发现按照官方文档的顺序反而会更好些,因为不必去后边的章节去为当前章节去打基础,所以我们先学习Spri ...

  8. MongoDB-3.4搭建副本集

    搭建副本集 1:首先创建3台虚拟机作为配置环境 IP1:192.168.101.175 IP2:192.168.101.176 IP3:192.168.101.177 2.下载MongoDB 3.4版 ...

  9. HDU 1251 统计难题 字符匹配

    题目描述:先说明此题只有一个测试实例,然后输入一系列的单词,以一个回车为结束符,然后输入一个字符串,要你查找以这个字符串为前缀的单词的个数,处理到文件结束. 解题报告:一开始看到这题,竟然直接用暴力去 ...

  10. 第12月第29天 cocos quick manual

    1. Example: $ cd cocos2d-x $ ./setup.py $ source FILE_TO_SAVE_SYSTEM_VARIABLE $ cocos new MyGame -p ...