[LeetCode] (medium) 240. Search a 2D Matrix II

https://leetcode.com/problems/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 ascending from top to bottom.

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.


说到有序数组查找,第一反应当然就是二分。我一开始想的是对角线二分,然后可以将矩阵分为四块,左上和右下可以直接派出,在左下和右上两块递归查找,看上去好像还行,但是当查找的矩阵不是方阵时,对角线二分会频繁递归(因为对交线上元素本来就很少),最极端情况是1*n(或n*1)对角线元素只有一个。

之后考虑对搜索区间的第一行进行普通二分,可以将右半侧排除,对左半侧递归深入,考虑到搜索区间会越来越窄,使用相同的方法对区间的第一列进行二分,每次递归根据搜索区间的形状选择使用哪种搜索方法。但是效率极低,需要400+ms 

class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        if(matrix.size() == 0) return false;
        
        int row = matrix.size();
        int col = matrix[0].size();
        
        pair<int, int> lo(0, 0);
        pair<int, int> hi(row-1, col-1);
        
        if(row > col) return search_col(matrix, target, lo, hi);
        else return search_row(matrix, target, lo, hi);
    }
    
    bool search_row(vector<vector<int>>& matrix, int target, pair<int, int> lo, pair<int, int> hi){
        if(lo.first > hi.first || lo.second > hi.second) return false;
        
        int st = lo.second;
        int ed = hi.second+1;
        int mid;
        
        while(st < ed){
            mid = (st + ed)/2;
            if(matrix[lo.first][mid] > target){
                ed = mid;
            }else{
                st = mid+1;
            }
        }
        if(st > 0 && matrix[lo.first][st-1] == target) return true;
        else{
            pair<int, int> new_lo(lo.first+1, lo.second);
            pair<int, int> new_hi(hi.first, st-1);
            if(new_hi.first-new_lo.first > new_hi.second-new_lo.second){
                return search_col(matrix, target, new_lo, new_hi);
            }else{
                return search_row(matrix, target, new_lo, new_hi);
            }
        }
    }
    
    bool search_col(vector<vector<int>>& matrix, int target, pair<int, int> lo, pair<int, int> hi){
        if(lo.first > hi.first || lo.second > hi.second) return false;
        
        int st = lo.first;
        int ed = hi.first+1;
        int mid;
        
        while(st < ed){
            mid = (st + ed)/2;
            if(matrix[mid][lo.second] > target){
                ed = mid;
            }else{
                st = mid+1;
            }
        }
        if(st > 0 && matrix[st-1][lo.second] == target) return true;
        else{
            pair<int, int> new_lo(lo.first, lo.second+1);
            pair<int, int> new_hi(st-1, hi.second);
            if(new_hi.first-new_lo.first > new_hi.second-new_lo.second){
                return search_col(matrix, target, new_lo, new_hi);
            }else{
                return search_row(matrix, target, new_lo, new_hi);
            }
        }
    }
};

看了别人的答案才知道原来这道题用的不是二分,利用原始矩阵的特性,我们可以知道一个元素左上方的部分都小于它,右下方的部分都大于它,直观上来想我们可以利用一次比较不断扩充这两个区域(将元素批量化的排除搜索区域),所以最开始的时候这两部分都为空,即此时的活跃元素位于矩阵的最右上角。

从另一个角度想,当前活跃元素在经过比较后,其左上方与右下方必然有一个区域能够确定相对target的性质,但是我们之所以不能简单地直接移动边界是因为其右上方的部分的性质是没法确定的,因此我们只要始终确保活跃元素位于搜索区间的最右上方,就能使难以确定的部分始终为空,使得每次都可以直接移动边界。

class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        static int fast_io = []() { std::ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }();
        if(matrix.size() == 0 || matrix[0].size() == 0) return false;
        
        int row = matrix.size();
        int col = matrix[0].size();
        
        int cur_row = 0, cur_col = col-1;
        
        while(cur_row < row && cur_col >= 0){
            if(matrix[cur_row][cur_col] == target) return true;
            else if(matrix[cur_row][cur_col] > target) --cur_col;
            else ++cur_row;
        }
        
        return false;
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值