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 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.
Consider the following matrix:
[
[1, 3, 5, 7],
[2, 4, 7, 8],
[3, 5, 9, 10]
]
Given target = 3, return 2.
O(m+n) time and O(1) extra space
解题思路:
从左下角往右上角找,若是小于target就往右找,若是大于target就往上找。时间复杂度O(m+n) n 为行数,m为列数。
定义count 计数。
与 Leetcode 240. Search a 2D Matrix II 类似。
Java code:
public class Solution {
/**
* @param matrix: A list of lists of integers
* @param: A number you want to search in the matrix
* @return: An integer indicate the occurrence of target in the given matrix
*/
public int searchMatrix(int[][] matrix, int target) {
//check corner case
if (matrix == null || matrix.length == 0) {
return 0;
}
if (matrix[0] == null || matrix[0].length == 0) {
return 0;
}
//find from bottom left to top right
int n = matrix.length; //row
int m = matrix[0].length; //column
int x = n - 1;
int y = 0;
int count = 0;
while (x >= 0 && y < m) {
if (matrix[x][y] < target) {
y++;
} else if (matrix[x][y] > target) {
x--;
} else {
count++;
x--;
y++;
}
}
return count;
}
}
Reference:
1. http://www.jiuzhang.com/solutions/search-a-2d-matrix-ii/
LintCode 38. Search a 2D Matrix II的更多相关文章
- 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, ret ...
- leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II
74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...
- 【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 ...
- 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 ...
- Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)
Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...
- LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37
240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...
- 【刷题-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 ...
- [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 ...
- LeetCode Search a 2D Matrix II
原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...
随机推荐
- (六)Hibernate 映射类型
所有项目导入对应的hibernate的jar包.mysql的jar包和添加每次都需要用到的HibernateUtil.java 第一节:基本类型映射 例子: hibernate.cfg.xml < ...
- iOS 制作发布证书,发布到App Store
---恢复内容开始--- 1.登陆 iOS Dev Center 选择进入iOS Provisioning Portal. 2.在 iOS Provisioning Portal中,点击App IDs ...
- mysql error笔记1
mysql视图问题: The user specified as a definer ('root'@'%') does not exist 原因:由于root用户对全局host无访问权限,给root ...
- spark-shell 执行脚本并传入参数
使用方法: ./spark-script.sh your_file.scala first_arg second_arg third_arg 脚本: scala_file=$ arguments=$@ ...
- ECMAScript位操作符
在ECMAScript中,有少数的几个操作符可以对二进制位进行直接操作,这几个操作符本身直接对二进制进行操作,所有它们的本身是非常效率的,学习这一段有助于以后的优化以及理解. ECMAScript中采 ...
- PHP保存base64
base64图片格式:$base64_url = data:image/jpeg;base64,xxxxxxxxxxxxxxxxxxxxxx 1,去除头部:$base64_body = substr( ...
- Linux - tar命令过滤某个目录
tar -zcvf abc.20140325.tar.gz --exclude=./abc/kkk/--exclude=./abc/hhh/ ./abc/ 发现没有过滤成功,后来发现这种方法是不对的( ...
- 《工作型PPT设计之道》培训心得
参加包翔老师的“工作型PPT设计之道>培训,颇多心得,后来为部门新员工和同组同事做了转化培训,将心得整理成一份PPT讲义,效果颇佳.现将主要心得整理于此.因时间仓促,24条心得有拼凑之嫌,有待今 ...
- 【 java版坦克大战--事件处理】 键盘控制小球上下左右移动
上一节已经学习了事件处理,这一节需要完成通过键盘的上下左右键控制小球移动. 然后再通过应用到我们绘制的坦克上. /** * 加深对事件处理机制的理解 * 通过光标的上下左右键,控制小球的左右上下移动. ...
- C语言-07其它相关
预处理指令 /* 不带参数的宏定义 1.所有的预处理指令都是以#开头 2.预处理指令分3种 1> 宏定义 2> 条件编译 3> 文件包含 3.预处理指令在代码翻译成0和1之前执行 4 ...