[Leetcode]Search a 2D Matrix II

原创 2015年11月19日 14:36:08

Search a 2D Matrix II My Submissions Question
Total Accepted: 19946 Total Submissions: 64757 Difficulty: Medium
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.

Subscribe to see which companies asked this question

当时看到这道题 我的想法是这样的,如下代码(p.s 实现的时候遇到了各种细节问题,下面是discuss中的)

class Solution {
public:
    bool search(vector<vector<int>> &matrix,int si,int ei,int sj,int ej,int target){

        if(si>ei || sj>ej || matrix[si][sj] > target || matrix[ei][ej] < target)  return false;
    //si is up side,ei is down side,sj is left side, ej is right side 
        int mi = (si + ei) / 2;
        int mj = (sj + ej) / 2;

        if(matrix[mi][mj] == target)
            return true;
        else if(matrix[mi][mj] < target){
            return search(matrix,si,mi,mj+1,ej,target) ||search(matrix,mi+1,ei,sj,mj,target)||search(matrix,mi+1,ei,mj+1,ej,target);
        }
        else{
            return search(matrix,si,mi-1,mj,ej,target) ||search(matrix,mi,ei,sj,mj-1,target) ||search(matrix,si,mi-1,sj,mj-1,target);
        }
    }
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        if(matrix.empty()) return false;

        return search(matrix,0,matrix.size()-1,0,matrix[0].size()-1,target);
    }
};

后来看到大神写的是这样的。。。

 bool searchMatrix(vector<vector<int>>& matrix, int target) {
    int m = matrix.size(), n = matrix[0].size();
    int i = 0, j = n - 1;

    // search target from up-right side to down-left side
    while (i <= m-1 && j >= 0) {
        if (matrix[i][j] == target)
            return true;
        else if (matrix[i][j] > target)
            --j;
        else // matrix[i][j] < target
            ++i;
    }

    return false;
}

果然还停留在菜鸟水平,做完怀疑了一晚的智商

版权声明:转载请注明出处

相关文章推荐

LeetCode Search a 2D Matrix II

Description: Write an efficient algorithm that searches for a value in an m x n matrix. This ma...

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

Java 实现leetcode Search a 2D Matrix II

Java 实现leetcode Search a 2D Matrix II本片依旧是关于Leetcode 里面算法题目的详解,实现Search a 2D Matrix II问题算法 问题描述:在这个...

Leetcode Search a 2D Matrix II

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

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

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

LeetCode Search a 2D Matrix II

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

[leetcode] 240 Search a 2D Matrix II

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

<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]Search a 2D Matrix II
举报原因:
原因补充:

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