题目描述

LeetCode 74. 搜索二维矩阵

题解

class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        bool found = false;
        
        if(matrix.empty()) return false;
        
        int rows = matrix.size();
        int cols = matrix[0].size();
        
        int row = 0;
        int col = cols - 1;
        while(row < rows && col >= 0) {
            if(matrix[row][col] == target) {
                found = true;
                break;
            }
            else if(matrix[row][col] > target) --col;
            else ++row;
        }
        return found;
    }
};

相关文章:

  • 2021-06-24
  • 2021-12-03
  • 2021-08-06
  • 2022-03-05
  • 2021-06-23
  • 2021-05-13
  • 2021-10-14
  • 2021-05-15
猜你喜欢
  • 2021-10-10
  • 2021-04-17
  • 2021-06-09
  • 2021-08-09
  • 2021-11-01
  • 2022-02-14
相关资源
相似解决方案