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 ...
随机推荐
- hihocoder 1037 数字三角形
#1037 : 数字三角形 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 问题描述 小Hi和小Ho在经历了螃蟹先生的任务之后被奖励了一次出国旅游的机会,于是他们来到了大洋彼岸 ...
- js获取iframe的parent对象
使用谷歌浏览器调试代码时无意间发现了一个奇特的问题:从iframe页面调用父级页面的方法,window.parent.text(),出现 Blocked a frame with origin &qu ...
- 关于近期需要学习sqlserver2008
在胜利油田物探院借了一本书叫做 sqlserver2008宝典 第二版,但是看不下去 下面打算直接安装数据库上手了,边练边学
- SQL_UNPIVOT(行列转换)
--临时表 insert into ##table([column1],S1, S2,S3)VALUES('VALUE','VALUE','VALUE','VALUE') --把原S1, S2,S3列 ...
- sql Truncate 与 delete的区别
Truncate 语法 TRUNCATE TABLE [ { database_name .[ schema_name ] . | schema_name . } ] table_na ...
- ocx在我indows7无法注册
公司今天用到一个 要用到ocx ,我调试好久都无法安装..... 后来在网上看到.原来是没有安装 VC Redist Installer(VC20052008201020122013)运行库合集 导 ...
- dataAdapter与dataSet和dataTable的填充
对于dataAdapter与dataSet和dataTable的填充,可以分为1对1,1对n,n对n,3种情况. 以SqlDataAdapter为例. //(1)1对1 SqlDataAdapter ...
- 第十一篇、微信小程序-input组件
主要属性: 效果图: ml: <!--style的优先级比class高会覆盖和class相同属性--> <!--头像--> <view style="displ ...
- 20171107--SQL变量,运算符,存储过程
create database shujuku03 go use shujuku03 go create table jiaoshi--创建jiaoshi表-- ( code int primary ...
- 1. 走进java
走进java 1.java简介 1.1 java分类: 1.2 开发包JDK 1.3.一处编译,到处运行的特点 2. 配置开发环境 3. java基本语法 3.1关键字和保留字 3.2 标识符命名规则 ...