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. jquery简单的图片切换效果,支持pc端、移动端的banner图片切换开发

    详细内容请点击 无意中看见了两年前写的一个图片切换,那会儿刚刚学习网页制作,可以说是我的第一个处女座的jquery图片切换效果.无聊之余对它的宽度稍稍做了一下修改,变成了支持pc端.手机端全屏的ban ...

  2. Android渲染机制和丢帧分析

    http://blog.csdn.net/bd_zengxinxin/article/details/52525781 自己编写App的时候,有时会感觉界面卡顿,尤其是自定义View的时候,大多数是因 ...

  3. Windows 10 (or 8)Chrome 观看视频发生flash不能加载,即"could't load plugins"原因之一

    最近一直如题,不能看视频,后来发现从一个已经使用管理员权限打开的应用转到Chrome就可以加载flash,而从桌面打开Chrome就加载不了. 今天再次查找信息,从Ubuntu下Chrome不能加载f ...

  4. iOS 自定义view里实现控制器的跳转

    1.view里实现控制器的modal 拿到主窗口的根控制器,用根控制器进行modal需要的modal的控制器 场景:点击自定义view里的按钮实现控制器的modal UIViewController ...

  5. PHP调用WebService

    1.  环境配置 配置php.ini,把php_soap.dll前面的分号去掉, 配置完成,需要重启. 2.  PHP调用代码,如下 <?php try { $soap = new SoapCl ...

  6. 20141017--异常语句try-catch

    //try-catch 尝试(try)-抓获(catch) try//尝试,保护起来,使程序出错也能执行 { //确定不会出错时不要用try,当不确定时使用try-catch可以捕获错误, int i ...

  7. 8个超炫酷的纯CSS3动画及源码分享

    在现代网页中,我们已经越来越习惯使用大量的CSS3元素,而现在的浏览器也基本都支持CSS3,所以很多时候我们不妨思考一下是否可以用纯CSS3制作一些有趣或者实用的网页.本文要分享8个超炫酷的纯CSS3 ...

  8. Linux驱动编程--基于I2C子系统的I2C驱动的Makefile

    ifeq ($(KERNELRELEASE),) KERNELDIR ?= /lib/modules/$(shell uname -r)/buildPWD := $(shell pwd) TEST = ...

  9. zedboard 构建嵌入式linux

    本文通过五部完成zedboard的嵌入式LINUX搭建,所谓磨刀不五砍材工嘛 1:系统环境搭建 要准备好交叉编译环境 见http://blog.csdn.net/xiabodan/article/de ...

  10. shell 字符截取

    Linux 的字符串截取很有用.有八种方法. 假设有变量 var=http://www.aaa.com/123.htm. # 读sharp 谐音 杀: ${var#*//} 杀掉//左边的,保留右边的 ...