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 ...
随机推荐
- Hartree-Fock理论(更新中)
预备知识: 基组 分子轨道基本概念与Hartree Product 平均场与Fock算符 在忽略分子中电子的相互作用时,我们有了一个粗糙的模型,虽然非常容易求解,但是描述的精确程度非常差. 考虑电子的 ...
- Bootstrap基础学习-1
Bootstrap是一个基于栅格结构的前端结构框架(当然也有JS,JQuery),它的优点是内容框架能够迅速搭建起来,基于媒介查询可以使搭建的页面迅速的适应不同的用户端,无论是手机,平板,还是PC,基 ...
- WebService 的创建,部署和使用
WebService,即Web服务,能使得运行在不同机器上的不同应用无须借助,专门的第三方软件或硬件,就可相互交换数据或集成. 第一次选择WebService,是为了替代数据库远程连接.我们都知道当S ...
- PHP使用COM 获取RTF内容
1. 需要在APP服务器上安装Office 2. 可能需要创建文件夹:Desktop C:\Windows\SysWOW64\config\systemprofile\Desktop 3. 可能需要设 ...
- Cocos2d-x移植到WindowsPhone8移植问题-libNetwork库移植问题
Cocos2d-x提供了libNetwork库的源代码,而且还提供了libNetwork库的源代码Visual Studio 2012工程文件libNetwork.vcxproj,这样就更方便移植了. ...
- WPF DataGrid 操作列 类似 LinkButton
WPF中没有类似LinkButton,所以只有运用Button及样式来实现LinkButton. DataGrid 操作列 实现 多个类似LinkButton按钮: 具体实现代码如下: <Dat ...
- UI2_UITextField
// // ViewController.h // UI2_UITextField // // Created by zhangxueming on 15/7/2. // Copyright (c) ...
- webSphere中文日志乱码,设置日志编码方法
1:管理控制台--->服务器--->应用程序服务器--->server1--->java和进程管理--->进程定义--->java虚拟机--->将通用jvm ...
- 怎样把function中的arguments变成普通数组
当我们在写一个具有处理可变长度参数的函数时,需要对arguments做一些操作.但是arguments它并不是一个数组,没有数组的各种操作,而且,JS的严格模式中不允许更改它的值. 这时我们需要将它的 ...
- GDAL读取tiff文件/C++源码
// gdal_geotiff.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include "gdal_priv.h&quo ...