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.

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:

 1 public class Solution {
 2     /**
 3      * @param matrix: A list of lists of integers
 4      * @param: A number you want to search in the matrix
 5      * @return: An integer indicate the occurrence of target in the given matrix
 6      */
 7     public int searchMatrix(ArrayList<ArrayList<Integer>> matrix, int target) {
 8         int m = matrix.size();
 9         if (m==0) return 0;
10         int n = matrix.get(0).size();
11         if (n==0) return 0;
12
13         return searchMatrixRecur(matrix,target,0,0,m-1,n-1);
14     }
15
16     public int searchMatrixRecur(ArrayList<ArrayList<Integer>> matrix, int target, int x1, int y1, int x2, int y2){
17         if (x2<x1 || y2<y1) return 0;
18
19         if (x1==x2 && y1==y2)
20             if (matrix.get(x1).get(y1)==target) return 1;
21             else return 0;
22
23         int midX = (x1+x2)/2;
24         int midY = (y1+y2)/2;
25         int midVal = matrix.get(midX).get(midY);
26         int res = 0;
27
28         if (midVal==target){
29             //We have to search all the four sub matrix.
30             res++;
31             res += searchMatrixRecur(matrix,target,x1,y1,midX-1,midY-1);
32             res += searchMatrixRecur(matrix,target,midX+1,midY+1,x2,y2);
33             res += searchMatrixRecur(matrix,target,(x1+x2)/2+1,y1,x2,(y1+y2)/2-1);
34             res += searchMatrixRecur(matrix,target,x1,(y1+y2)/2+1,(x1+x2)/2-1,y2);
35         } else if (midVal>target) {
36             int leftX = (x1+x2)/2;
37             int leftY = y1;
38             int upX = x1;
39             int upY = (y1+y2)/2;
40             if (target==matrix.get(leftX).get(leftY)) res++;
41             if (target==matrix.get(upX).get(upY)) res++;
42             if (target <= matrix.get(leftX).get(leftY) && target <=matrix.get(upX).get(upY)){
43                 res += searchMatrixRecur(matrix,target,x1,y1,midX-1,midY-1);
44             } else if (target <= matrix.get(leftX).get(leftY)){
45                 res += searchMatrixRecur(matrix,target,x1,y1,(x1+x2)/2-1,y2);
46             } else if (target <= matrix.get(upX).get(upY)){
47                 res += searchMatrixRecur(matrix,target,x1,y1,x2,(y1+y2)/2-1);
48             } else {
49                 res += searchMatrixRecur(matrix,target,x1,y1,x2,(y1+y2)/2-1);
50                 res += searchMatrixRecur(matrix,target,upX,upY,(x1+x2)/2-1,y2);
51             }
52         } else {
53             int rightX = (x1+x2)/2;
54             int rightY = y2;
55             int lowX = x2;
56             int lowY = (y1+y2)/2;
57             if (target==matrix.get(rightX).get(rightY)) res++;
58             if (target==matrix.get(lowX).get(lowY)) res++;
59             if (target >= matrix.get(rightX).get(rightY) && target >= matrix.get(lowX).get(lowY)){
60                 res += searchMatrixRecur(matrix,target,midX+1,midY+1,x2,y2);
61             } else if (target >= matrix.get(rightX).get(rightY)){
62                 res += searchMatrixRecur(matrix,target, (x1+x2)/2+1,y1,x2,y2);
63             } else if (target >= matrix.get(lowX).get(lowY)){
64                 res += searchMatrixRecur(matrix,target, x1, (y1+y2)/2+1, x2, y2);
65             } else {
66                 res += searchMatrixRecur(matrix,target, (x1+x2)/2+1,y1, lowX, lowY);
67                 res += searchMatrixRecur(matrix,target, x1, (y1+y2)/2+1, x2, y2);
68             }
69
70         }
71         return res;
72     }
73 }
时间: 2024-10-19 06:06:28

LintCode-Search 2D Matrix II的相关文章

leetcode Search a 2D Matrix II

题目连接 https://leetcode.com/problems/search-a-2d-matrix-ii/ Search a 2D Matrix II Description Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascend

【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. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascendin

240.Search in a 2D Matrix II

/* * 240.Search in a 2D Matrix II * 2016-6-17by Mingyang * From left-bottom to right-top * 他这道题目虽说是用了BS,但是并不一定要有mid,这里就从最左下角到右上角 * 这个数大于同一列的,小于同一行的,只要跟target比较以后,就可以要么删除一列,要么删除一行--------找拐点 * 这个题目自己做的时候给出了一个nlog的方案,先是row从下往上走,在同一row里面再继续bs * 但是这个题目的好

LeetCode -- Search a 2D Matrix &amp; Search a 2D Matrix II

Question: Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the la

Search a 2D Matrix II【原创】

Search a 2D Matrix II: python学了还么两周可能有些地方写的很新手大家不要笑我 解题思路: 首先当数组为1维数组时if in 直接查找即可 当为 m x n 维数组时: matrix为行增列增的有序矩阵 所以对行和列采用折半查找,即可判断出值所在的新的矩阵范围,一步步缩小,若存在则某一次的行折半或者列折半肯定会遍历到,否则不存在于矩阵中 每一行的第一个元素为最小值,每一列最后一个元素为最大值 若此行的第一个元素比target要大,那肯定在行号小于此行的行中,若此列的最后

[LeetCode][JavaScript]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. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascendin

刷题240. Search a 2D Matrix II

一.题目说明 题目240. Search a 2D Matrix II,从一个m*n的二维矩阵查找一个整数,每一行从左到右递增,每一列从上到下递增. 二.我的解答 先计算矩阵中点matrix[row_mid][col_mid],然后将矩阵分成4个区间: class Solution{ public: bool dfs(vector<vector<int> >& matrix,int target,int start_x, int end_x, int start_y, in

lintcode 中等题:search a 2d matrix II 搜索二维矩阵II

题目 搜索二维矩阵 II 写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每一列的整数从上到下是排序的. 在每一行或每一列中没有重复的整数. 样例 考虑下列矩阵: [     [1, 3, 5, 7],     [2, 4, 7, 8],     [3, 5, 9, 10] ] 给出target = 3,返回 2 挑战 要求O(m+n) 时间复杂度和O(1) 额外空间 解题 直接遍历,时间复杂度是O(MN) public

【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 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 bo

LintCode &quot;Search a 2D Matrix II&quot;

Simply a revise of a genius Greedy algorithm seen on LeetCode - linear walking. class Solution { public: /** * @param matrix: A list of lists of integers * @param target: An integer you want to search in matrix * @return: An integer indicate the tota