原题链接在这里:https://leetcode.com/problems/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.

题解:

本题是每一行每一列都是升序,从右上角开始x = matrix[i][j], i = 0, j = matrix[0].length - 1, 用x 和target 比较,若是相等则返回true.

若是小于target, 说明肯定不会在这一行,所以i++. 若是大于target, 说明必然不会在这一列所以 j--.

若走到边界还没有返回说明没有这个target, 返回false.

Note: j的初始量是 j = n-1, 注意减一.

Time Complexity: O(m+n). m = matrix.length. n = matrix[0].length.

Space: O(1).

AC Java:

 public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix == null || matrix.length == 0 || matrix[0].length == 0){
return false;
}
int m = matrix.length;
int n = matrix[0].length;
int i = 0;
int j = n-1; //error
while(i<m && j>=0){
int x = matrix[i][j];
if(x == target){
return true;
}else if(x < target){
i++;
}else{
j--;
}
}
return false;
}
}

类似Search a 2D Matrix.

跟上Kth Smallest Element in a Sorted Matrix.

LeetCode Search a 2D Matrix II的更多相关文章

  1. [LeetCode] 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 ...

  2. LeetCode Search a 2D Matrix II (技巧)

    题意: 有一个矩阵,每行有序,每列也有序.判断一个数target是否存在于此矩阵中. 思路: 从右上角开始,如果当前数字<target,则该行作废.若当前数字>target,该列作废.这样 ...

  3. LeetCode——Search a 2D Matrix II

    Description: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix ...

  4. Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)

    Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...

  5. LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37

    240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...

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

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

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

  8. LeetCode -- Search a 2D Matrix & Search a 2D Matrix II

    Question: Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matr ...

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

随机推荐

  1. RGB

    一,介绍 RGB色彩模式是工业界的一种颜色标准,是通过对红(R).绿(G).蓝(B)三个颜色通道的变化以及它们相互之间的叠加来得到各式各样的颜色的, RGB即是代表红.绿.蓝三个通道的颜色,这个标准几 ...

  2. Redis中7种集合类型应用场景

    StringsStrings 数据结构是简单的key-value类型,value其实不仅是String,也可以是数字.使用Strings类型,你可以完全实现目前 Memcached 的功能,并且效率更 ...

  3. C语言根据日期(年,月,日)判断星期几(使用基姆拉尔森计算公式)

    C语言根据日期判断星期几(使用基姆拉尔森计算公式) 算法如下: 基姆拉尔森计算公式W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7 在公式中d表示日期中的日数, ...

  4. QLineEdit 使用方法

    在Qt中,QLineEdit是文本编辑框控件,是比较基础且常用的控件的之一,下面是其的一些基本操作. 比如: 禁用或启用该控件 // lineedit is the object name of QL ...

  5. zabbix配置文件详解

    Zabbix之配置文件详解   zabbix配置文件种类: zabbix_server配置文件zabbix_server.conf zabbix_proxy配置文件zabbix_proxy.conf ...

  6. 未能找到Microsoft.Office.Core.MsoTriState的引用

    1.首先似乎没有找到这个引用,然后根据网上的教程,添加了Microsoft Office 12.0 Library,根据对象浏览器,查看此dll,有core,也有 MsoTriState也有,编译的时 ...

  7. hdu Bone Collector

    入门级动态规划 #include"iostream" #include"string.h" #include"stdio.h" using ...

  8. CSV to XLSX (专用)

    $csvFile = "F:\ACL\HZ ACL\ACL-APAC.CSV" $path = "F:\ACL\HZ ACL\ACL-APAC.XLSX" $r ...

  9. 【iCore2模块】VGA模块样板谍照!

    基于 iCore2 双核心板的 VGA模块样机做出来好久了,经过一个多月的努力奋战,该模块的代码已经写完,硬件也测试完毕,性能很好.下面贴几张图: 照片一: 为了节约时间,打样用的是绿色的板子,不过批 ...

  10. LR性能测试脚本增强与调试

    脚本增强与调试 一般来说,使用LR的Vugen录制的脚本并不能直接用于测试,需要对脚本进行各方面的增强,主要包括添加注释.关联.检查点.事务.参数化.日志输出等.下面结合刚完成的一个web项目性能测试 ...