Search a 2D Matrix II

Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence of it.

This matrix has the following properties:

  • Integers in each row are sorted from left to right.
  • Integers in each column are sorted from up to bottom.
  • No duplicate integers in each row or column.
Example

Consider the following matrix:

[
[1, 3, 5, 7],
[2, 4, 7, 8],
[3, 5, 9, 10]
]

Given target = 3, return 2.

分析:

因为数组里的数有上面三个特性,所以我们可以从左下角开始找。如果当前值比target大,明显往上走,比target小,往右走,如果一样斜上走。Time complexity: (O(m + n)) (m = matrix.length, n = matrix[0].length)

 public class Solution {
public int searchMatrix(int[][] matrix, int target) {
// check corner case
if (matrix == null || matrix.length == 0 || matrix[].length == 0) {
return ;
}
// from bottom left to top right
int x = matrix.length - ;
int y = ;
int count = ; while (x >= && y < matrix[].length) {
if (matrix[x][y] < target) {
y++;
} else if (matrix[x][y] > target) {
x--;
} else {
count++;
x--;
y++;
}
}
return count; }
}

Search a 2D Matrix I

Write an efficient algorithm that searches for a value in an mn 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

Consider the following matrix:

[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]

Given target = 3, return true.

分析:

根据数组的特点,我们需要找出一个大范围(row),然后再确定小范围。

 public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == || matrix[].length == ) return false;
int rowCount = matrix.length, colCount = matrix[].length;
if (matrix[][] > target || matrix[rowCount - ][colCount - ] < target) return false; int row = getRowNumber(matrix, target);
int column = getColumnNumber(matrix, row, target); return matrix[row][column] == target; } public int getRowNumber(int[][] matrix, int target) {
int start = , end = matrix.length - , width = matrix[].length;
while (start <= end) {
int mid = start + (end - start) / ;
if (matrix[mid][width - ] == target) {
return mid;
} else if (matrix[mid][width - ] > target) {
end = mid - ;
} else {
start = mid + ;
}
}
return start;
} public int getColumnNumber(int[][] matrix, int row, int target) {
int start = , end = matrix[].length; while (start <= end) {
int mid = start + (end - start) / ;
if (matrix[row][mid] == target) {
return mid;
} else if (matrix[row][mid] > target) {
end = mid - ;
} else {
start = mid + ;
}
}
return start;
}
}

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

  1. LintCode 38. Search a 2D Matrix II

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

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

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

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

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

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

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

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

  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 II 搜索一个二维矩阵之二

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

  9. LeetCode Search a 2D Matrix II

    原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...

随机推荐

  1. Spring 使用中的设计模式

    1. 简单工厂 又叫做静态工厂方法(StaticFactory Method)模式,但不属于23种GOF设计模式之一. 简单工厂模式的实质是由一个工厂类根据传入的参数,动态决定应该创建哪一个产品类. ...

  2. 【CodeForces 297C】Splitting the Uniqueness

    题意 序列s有n个数,每个数都是不同的,把它每个数分成两个数,组成两个序列a和b,使ab序列各自去掉个数后各自的其它数字都不同. 如果存在一个划分,就输出YES,并且输出两个序列,否则输出NO. 分析 ...

  3. xbz分组题B 吉利数字 数位dp入门

    B吉利数字时限:1s [题目描述]算卦大湿biboyouyun最近得出一个神奇的结论,如果一个数字,它的各个数位相加能够被10整除,则称它为吉利数.现在叫你计算某个区间内有多少个吉利数字. [输入]第 ...

  4. gotoTop返回顶部 JS

    方法: 1.首先在body添加一个标签,在一个页面添加,其它页面也会生效. <body> <a name="top"> 2.然后在页脚添加一个链接 < ...

  5. 全程图解 手把手教您开启windows终端服务

    一.什么是远程桌面? 远程桌面是微软公司为了方便网络管理员管理维护服务器而推出的一项服务.从windows 2000 server版本开始引入,网络管理员使用远程桌面连接程序连接到网络任意一台开启了远 ...

  6. PHP大批量插入数据库的3种方法和速度对比

    第一种,使用insert into 插入,最后显示为:23:25:05 01:32:05 也就是花了2个小时多! $params = array(‘value'=>'50′); set_time ...

  7. svn代码回滚命令

    代码回滚提交: 比如要把73回滚到68 svn merge -r 73:68 http://my.repository.com/my/project/trunk 然后commit就行了 svn com ...

  8. CSS创建一个遮罩层

    .layer{ width: 100%; position: absolute; left:; right:; top:; bottom:; -moz-opacity:; filter: alpha( ...

  9. gnl总结(#,%,$)

    Ognl表达式struts标签“%,#,$” 1.什么是Ognl? OGNL(Object-Graphic Navigation Language),对象图道行语言.是一种可以方便操作对象属性的开源表 ...

  10. Java&.Net虚拟机精简(GreenJVM&GreenDotNet发布) .

    精简JRE体积的小工具:http://blog.csdn.net/cping1982/archive/2008/09/02/2865198.aspx 项目地址:http://code.google.c ...