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. 解决系统打开CHM文件无法正常显示

    最近学习servlet下载了一个CHM的帮助手册但是打开后右侧却时空白.试了各种方法都没有成功最后终于找到原因所在. 一般情况下无法显示网页:右键 chm文件属性里最下面有个“解除锁定”,点击“解除锁 ...

  2. js 选项卡实现

    <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...

  3. BZOJ-2038 小Z的袜子(hose) 莫队算法

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec Memory Limit: 259 MB Submit: 5573 Solved: 2568 [Subm ...

  4. Spring JdbcTemplate 的使用与学习(转)

    紧接上一篇 (JdbcTemplate是线程安全的,因此可以配置一个简单的JdbcTemplate实例,将这个共享的实例注入到多个DAO类中.辅助的文档) Spring DAO支持 http://ww ...

  5. Ubuntu添加开机自动启动程序的方法

    文章出处:http://hi.baidu.com/gcc_gun/blog/item/fe9bbc4b84e911fa83025cb8.html 1. 开机启动时自动运行程序 Linux加载后, 它将 ...

  6. tmux下的滚屏

    先Ctrl+b进入tmux的操作模式,然后用PageUp和PageDown,

  7. DEDECMS全版本gotopage变量XSS ROOTKIT 0DAY

    影响版本: DEDECMS全版本 漏洞描叙: DEDECMS后台登陆模板中的gotopage变量未效验传入数据,导致XSS漏洞. \dede\templets\login.htm 65行左右 < ...

  8. php 获取后缀的几种方法

    1: function get_extension($file){ substr(strrchr($file, '.'), 1); } 2: function get_extension($file) ...

  9. (转) 新的开始之Win7、CentOS 6.4 双系统 硬盘安装

    http://blog.csdn.net/cnclenovo/article/details/11358447

  10. wordPress Development

    site:http://codex.wordpress.org/Theme_Development 2014-03-24 This article is about developing WordPr ...