leetcode_c++:Divide and Conquer: . Search a 2D Matrix II(240)

原创 2016年08月28日 15:35:10

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 ascending from top to bottom.
For example,

Consider the following matrix:


[
  [1,   4,  7, 11, 15],
  [2,   5,  8, 12, 19],
  [3,   6,  9, 16, 22],
  [10, 13, 14, 17, 24],
  [18, 21, 23, 26, 30]
]

Given target = 5, return true.

Given target = 20, return false.


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 total occurrence of target in the given matrix 
     */  
    int searchMatrix(vector<vector<int> > &matrix, int target) {  
        // write your code here  

        if (matrix.size() == 0)  
            return 0;  

        int count = 0;  
        int rows = matrix.size();  
        int colums = matrix[0].size();  

        int curRowPos = 0;  
        int curColumPos = colums-1;  

        while (curRowPos < rows && curColumPos >= 0)  
        {  
            if (matrix[curRowPos][curColumPos] > target)  
            {  
                curColumPos--;  
            }  
            else if (matrix[curRowPos][curColumPos] < target)  
            {  
                curRowPos++;  
            }  
            else  
            {  
                count++;  
                curColumPos--;  
            }  
        }  

        return count;  
    }  
};  
版权声明:本文为博主原创文章,未经博主允许不得转载。

LeetCode Devide & Conquer || Search a 2D Matrix II

本题来源于LeetCode分治算法题库,难度medium.

LeetCode OJ 240. Search a 2D Matrix II

LeetCode OJ 240. Search a 2D Matrix IIDescriptionWrite an efficient algorithm that searches for a va...

Leetcode 240. Search a 2D Matrix II

这道题是同学问我的时候看到的,抱着solo的心态也做了一遍 第一次用的是二分和分治(刚好今晚老师也讲了分治) 然而效果好像不太佳? 先放一发题目: Write an efficient alg...

LeetCode[240] Search a 2D Matrix II(动画演示)

目录目录 Problem Example Thought1 Thought2 Presentation 总结 Online Judge Search a 2D Matrix IIProblem: ...

[leetcode] 240 Search a 2D Matrix II

很经典的一道题,每行由左到右,每列由上到下升序排列,让我们采用高效的算法判断一个值是不是存在。 第一种方案:遍历行,若发现行首的元素 这个算法的最坏时间复杂度是O(n*logm)。当然如果你对行也...
  • NK_test
  • NK_test
  • 2015年08月19日 12:15
  • 981

<LeetCode OJ> 74. / 240. Search a 2D Matrix (I / II)

240. Search a 2D Matrix II My Submissions Question Total Accepted: 28527 Total Submissions: 8556...

Leetcode 240. Search a 2D Matrix II[medium]

题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the ...

LeetCode No.240 Search a 2D Matrix II

LeetCode No.240 Search a 2D Matrix II

LeetCode240. Search a 2D Matrix II二分查找+分治法

题意:一个矩阵,每一行都是升序,每一列都是升序。快速判断该矩阵是否有某个值。我起初是这样做的,但是超时了。class Solution { public: bool searchMatrix(...

[leetcode]240. Search a 2D Matrix II

题目 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has t...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode_c++:Divide and Conquer: . Search a 2D Matrix II(240)
举报原因:
原因补充:

(最多只允许输入30个字)