zoukankan      html  css  js  c++  java
  • 【LeetCode-数组】搜索二维矩阵

    题目描述

    编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值。该矩阵具有如下特性:

    • 每行中的整数从左到右按升序排列。
    • 每行的第一个整数大于前一行的最后一个整数。

    示例:

    输入:
    matrix = [
      [1,   3,  5,  7],
      [10, 11, 16, 20],
      [23, 30, 34, 50]
    ]
    target = 3
    输出: true
    
    输入:
    matrix = [
      [1,   3,  5,  7],
      [10, 11, 16, 20],
      [23, 30, 34, 50]
    ]
    target = 13
    输出: false
    

    题目链接: https://leetcode-cn.com/problems/search-a-2d-matrix/

    思路

    剑指 offer 上面的题目。从右上角或者左下角开始搜索,这里从右上角开始,最开始的位置为右上角 (curRow, curCol):

    • 如果 matrix[curRow][curCol]==target,则找到目标值返回 true;
    • 如果上面的条件不满足,开始循环,循环的条件是 matrix[curRow][curCol]!=target:
      - 如果 matrix[curRow][curCol]>target,那么应该将 curCol-- 寻找更小的值;如果 curCol<0,return false;
      - 如果 matrix[curRow][curCol]<target,那么应该将 curRow++ 寻找更大的值;如果 curRow>最大行数,return false;
      - 如果 matrix[curRow][curCol]=target,返回 true。

    代码如下:

    class Solution {
    public:
        bool searchMatrix(vector<vector<int>>& matrix, int target) {
            if(matrix.empty()) return false;
    
            int curRow = 0;
            int curCol = matrix[0].size()-1;
            if(curCol<0) return false;  // 防止二维数组是[[]]的情况
            if(matrix[curRow][curCol]==target) return true;  // 别忘了提前判断
    
            while(matrix[curRow][curCol]!=target){
                int curVal = matrix[curRow][curCol];
                if(matrix[curRow][curCol]>target){
                    curCol--;
                    if(curCol<0) return false;
                }
                if(matrix[curRow][curCol]<target){  // 不能用 else if
                    curRow++;
                    if(curRow>=matrix.size()) return false;
                }
                if(matrix[curRow][curCol]==target){ // 不能用 else if
                    return true;
                }
            }
            return false;   // 这里返回多少无关紧要,因为返回一定发生在while循环中或者while循环之前
        }
    };
    
    • 时间复杂度:O(n)
    • 空间复杂度:O(1)

    换一种写法
    可以将while的条件改一下,代码更加简洁:

    class Solution {
    public:
        bool searchMatrix(vector<vector<int>>& matrix, int target) {
            if(matrix.empty()) return false;
    
            int curRow = 0;
            int curCol = matrix[0].size()-1;
            if(curCol<0) return false;  // 防止二维数组是[[]]的情况
            
            while(curRow<matrix.size() && curCol>=0){
                if(matrix[curRow][curCol]>target) curCol--;
                else if(matrix[curRow][curCol]<target) curRow++;
                else if(matrix[curRow][curCol]==target) return true;
            }
            return false;
        }
    };
    
    • 时间复杂度:O(n)
    • 空间复杂度:O(1)
  • 相关阅读:
    HDU 6464 /// 权值线段树
    HDU 6468 /// DFS
    HDU 6469 /// 二分
    HDU 6470 /// 矩阵快速幂
    USACO 2014 US Open Odometer /// 数位DP
    hdu5988(费用流,对数相乘做加法)
    对AC自动机+DP题的一些汇总与一丝总结 (2)
    对AC自动机+DP题的一些汇总与一丝总结 (1)
    POJ 1625 Censored ( Trie图 && DP && 高精度 )
    HDU 2243 ( Trie图 矩阵构造幂和 )
  • 原文地址:https://www.cnblogs.com/flix/p/12839460.html
Copyright © 2011-2022 走看看