LintCode-Search 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
Solution:
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(ArrayList<ArrayList<Integer>> matrix, int target) {
int m = matrix.size();
if (m==0) return 0;
int n = matrix.get(0).size();
if (n==0) return 0;
return searchMatrixRecur(matrix,target,0,0,m-1,n-1);
}
public int searchMatrixRecur(ArrayList<ArrayList<Integer>> matrix, int target, int x1, int y1, int x2, int y2){
if (x2<x1 || y2<y1) return 0;
if (x1==x2 && y1==y2)
if (matrix.get(x1).get(y1)==target) return 1;
else return 0;
int midX = (x1+x2)/2;
int midY = (y1+y2)/2;
int midVal = matrix.get(midX).get(midY);
int res = 0;
if (midVal==target){
//We have to search all the four sub matrix.
res++;
res += searchMatrixRecur(matrix,target,x1,y1,midX-1,midY-1);
res += searchMatrixRecur(matrix,target,midX+1,midY+1,x2,y2);
res += searchMatrixRecur(matrix,target,(x1+x2)/2+1,y1,x2,(y1+y2)/2-1);
res += searchMatrixRecur(matrix,target,x1,(y1+y2)/2+1,(x1+x2)/2-1,y2);
} else if (midVal>target) {
int leftX = (x1+x2)/2;
int leftY = y1;
int upX = x1;
int upY = (y1+y2)/2;
if (target==matrix.get(leftX).get(leftY)) res++;
if (target==matrix.get(upX).get(upY)) res++;
if (target <= matrix.get(leftX).get(leftY) && target <=matrix.get(upX).get(upY)){
res += searchMatrixRecur(matrix,target,x1,y1,midX-1,midY-1);
} else if (target <= matrix.get(leftX).get(leftY)){
res += searchMatrixRecur(matrix,target,x1,y1,(x1+x2)/2-1,y2);
} else if (target <= matrix.get(upX).get(upY)){
res += searchMatrixRecur(matrix,target,x1,y1,x2,(y1+y2)/2-1);
} else {
res += searchMatrixRecur(matrix,target,x1,y1,x2,(y1+y2)/2-1);
res += searchMatrixRecur(matrix,target,upX,upY,(x1+x2)/2-1,y2);
}
} else {
int rightX = (x1+x2)/2;
int rightY = y2;
int lowX = x2;
int lowY = (y1+y2)/2;
if (target==matrix.get(rightX).get(rightY)) res++;
if (target==matrix.get(lowX).get(lowY)) res++;
if (target >= matrix.get(rightX).get(rightY) && target >= matrix.get(lowX).get(lowY)){
res += searchMatrixRecur(matrix,target,midX+1,midY+1,x2,y2);
} else if (target >= matrix.get(rightX).get(rightY)){
res += searchMatrixRecur(matrix,target, (x1+x2)/2+1,y1,x2,y2);
} else if (target >= matrix.get(lowX).get(lowY)){
res += searchMatrixRecur(matrix,target, x1, (y1+y2)/2+1, x2, y2);
} else {
res += searchMatrixRecur(matrix,target, (x1+x2)/2+1,y1, lowX, lowY);
res += searchMatrixRecur(matrix,target, x1, (y1+y2)/2+1, x2, y2);
}
}
return res;
}
}
LintCode-Search 2D Matrix II的更多相关文章
- 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 ...
- 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 ...
- 240.Search in a 2D Matrix II
/* * 240.Search in a 2D Matrix II * 2016-6-17by Mingyang * From left-bottom to right-top * 他这道题目虽说是用 ...
- 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 ...
- 【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 ...
随机推荐
- css优先机制
样式的优先级 (外部样式)External style sheet <(内部样式)Internal style sheet <(内联样式)Inline style (内部样式就是css写在 ...
- 提高HTML5 Canvas性能的技巧
详细内容请点击 一:使用缓存技术实现预绘制,减少重复绘制Canvs内容 很多时候我们在Canvas上绘制与更新,总是会保留一些不变的内容,对于这些内容 应该预先绘制缓存,而不是每次刷新. 直接绘制代码 ...
- asp动态生成google的sitemap地图的代码
本来使用那些网站生成google网站地图,时间久了,感觉太麻烦了:先打开他们的网站,输入我的网址,然后点击生成,等待一段时间后,下载生成后的文件,再将它通过ftp上传到空间上了.实在太麻烦了,还不如自 ...
- win7开启远程桌面
1.启用windows防火墙 计算机管理----->服务----->Windows Firewall(双击进入,启动类型改为自动,点击应用,点击启动)2.启动gpedit.msc打开“本地 ...
- ASP.NET C# 文件下载
1.文件下载到客户端 //WriteFile实现下载 protected void Download_Click(object sender, EventArgs e) { string fileNa ...
- CentOS7 固定ip
1. 进入/etc/ network-scripts/ 下ifcfg-eno16777736(文件名可能不一样,单前缀一般是ifcfg-eno) 2. vi打开 编辑 修改bootproro=&qu ...
- Swift字典
字典初始化 基本语法: [key 1: value 1, key 2: value 2, key 3: value 3] var airports: Dictionary<String ...
- MVC 模型js远程校验的使用方法
我们在网站注册的时候往往需要在用户注册完毕的时候显示用户名是否可用,这就要用到模型的远程校验了.具体如下. [Required(ErrorMessage = "用户名不能为空"), ...
- 20141201--测试 jQuery
测试 JavaScript 框架库 - jQuery jQuery 是一个 JavaScript 库. 引用 jQuery <!DOCTYPE html> <html> < ...
- Ajax_2
<script> var xmlHttp;//创建一个对象 function queryDetail(id){ if(window.ActiveXObject){ xmlHttp = ne ...