[LeetCode]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.

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.

[思路]O(m+n)复杂度,从右上开始搜寻。由于矩阵是有序的,所以可以根据大小关系缩小范围。

class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        int m = matrix.size();
        int n = matrix[0].size();
        int i = 0;
        int j = n-1;
        while(i>=0 && j>=0 && i<m &&j<n){
            if(target == matrix[i][j])
                return true;
            if(target>matrix[i][j]){
                i++;
            }
            else
                j--;
        }
        return false;
    }
};

O(mlogn)对每一行进行二分查找。

class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        int m = matrix.size();
        int n = matrix[0].size();
        for(int i=0 ; i<m ; ++i){
            if(matrix[i][0]<=target && matrix[i][n-1]>=target){
                if(searchVector(matrix[i],target))
                    return true;
            }
        }
        return false;
    }

   bool searchVector(vector<int>& v, int target) {
    int left = 0, right = v.size() - 1;
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (v[mid] == target)
            return true;
        if (v[mid] < target)
            left = mid + 1;
        else
         right = mid - 1;
        }

        return false;
    }

};

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-10 00:07:06

[LeetCode]Search a 2D Matrix II的相关文章

leetcode Search a 2D Matrix II

题目连接 https://leetcode.com/problems/search-a-2d-matrix-ii/ Search a 2D Matrix II Description 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 ascend

[LeetCode] 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.

LeetCode Search a 2D Matrix II (技巧)

题意: 有一个矩阵,每行有序,每列也有序.判断一个数target是否存在于此矩阵中. 思路: 从右上角开始,如果当前数字<target,则该行作废.若当前数字>target,该列作废.这样下去要么找到,要么到达边界退出. 1 class Solution { 2 public: 3 bool searchMatrix(vector<vector<int>>& matrix, int target) { 4 int n=matrix.size(), m=matri

[LeetCode][JavaScript]Search a 2D Matrix II

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 ascendin

【LeetCode】240. Search a 2D Matrix II

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 ascendin

LeetCode -- Search a 2D Matrix &amp; Search a 2D Matrix II

Question: Search a 2D Matrix 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 from left to right. The first integer of each row is greater than the la

leetcode——Search a 2D Matrix 二维有序数组查找(AC)

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 from left to right. The first integer of each row is greater than the last integer of the previous ro

[leetcode]Search a 2D Matrix @ Python

原题地址:https://oj.leetcode.com/problems/search-a-2d-matrix/ 题意: 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 from left to right. The first integer o

LeetCode: Search a 2D Matrix [074]

[题目] 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 from left to right. The first integer of each row is greater than the last integer of the previo