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;
    }
};
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 题目 LeetCode中文站 解答 这是一个二位矩阵搜索的题目,实际上就是一个二位数组,很简单,我们第一想到的方法...
    Aiewing阅读 4,314评论 0 0
  • 题目编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值。该矩阵具有如下特性: 每行中的整数从左到右按...
    HITZGD阅读 1,098评论 0 0
  • 题目描述 题解 两次遍历 时间复杂度为,空间复杂度为。 一次遍历 时间复杂度为,空间复杂度为。 二分法 时间复杂度...
    SmallRookie阅读 1,508评论 0 0
  • 正文
    灭绝师爷阅读 1,618评论 0 0
  • 产品在开发前期先要理解几个重要的问题:产品的用户是谁?他们的需求是什么?(用户需求)我们该如何去满足?(产品需求)...
    汪继志阅读 4,697评论 0 3