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.

Challenge

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的更多相关文章

  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. 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 ...

  3. leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II

    74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...

  4. 【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 ...

  5. 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 ...

  6. 240.Search in a 2D Matrix II

    /* * 240.Search in a 2D Matrix II * 2016-6-17by Mingyang * From left-bottom to right-top * 他这道题目虽说是用 ...

  7. Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)

    Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...

  8. LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37

    240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...

  9. 【刷题-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 ...

  10. 【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 ...

随机推荐

  1. 【Mongodb】---Scheme和Collections对应问题

    Mongodb通过mongoose来与数据进行操作.而mongoose是通过model来创建数据库中对应的collection mongoose.model('User', UserSchema); ...

  2. SQLite&&SharedPreferences&&IO读写Sdcard学习笔记

    SQLite 轻量级的.嵌入式的.关系型数据库 Android.IOS等广泛使用的的数据库系统 SQLite数据库之中可以方便的使用SQL语句,实现数据的增加.修改.删除.查询等操作 SQLiteOp ...

  3. CSS之transition(动画)

    Transform字面上就是变形,改变的意思.在CSS中transform主要包括以下几种:旋转rotate.扭曲skew.缩放scale和移动translate以及矩阵变形matrix. 下面我们一 ...

  4. MVC自定义错误页404静态页

    昨天公司要求给所有项目添加自定义404错误页,具体的要求实现的有以下几点: 1.实现自定义错误(如各种error,404等)跳转到指定的页面 2.所指定的页面输出的http状态值必须是404或其他指定 ...

  5. sql语句中charindex的用法 可用于截取字符串

    使用CHARINDEX和PATINDEX函数来搜索文字列和字符串.这两个函数是如何运转的,解释他们的区别.同时提供一些例子,通过这些例子,你可以可以考虑使用这两个函数来解决很多不同的字符搜索的问题. ...

  6. 用LINQ在集合中查询特定对象

    这里是原文出处: 简单的概括LINQ LINQ是Language-Integrated Query的缩写,是C# 3.0和VB 9.0中新加入的语言特性,可以在编程时使用内置的查询语言进行基于集合的操 ...

  7. Spring中Quartz的配置

    Quartz是一个强大的企业级任务调度框架,Spring中继承并简化了Quartz,下面就看看在Spring中怎样配置Quartz: 首先,来写一个测试被调度的类:(QuartzHelloWorldJ ...

  8. 使用jQuery POST提交数据返回的JSON是字符串不能解析为JSON对象

    post请求原代码: $.post( "/User/Home/Code", { Phone: $( "#phone").val() }, function (d ...

  9. css3 画x图形

    .m-act-del{ right:; top:; margin-top: -6px; position: absolute; display: inline-block; width: 20px; ...

  10. 为apache与mysql创建快捷方式

    为apache与mysql创建快捷方式 1)为apache创建快捷方式(软链接) 以后我们就可以在终端的任一位置,使用apachectl start|stop|restart   2)为mysql创建 ...