【Leetcode】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 last integer of the previous row.

For example,

Consider the following matrix:

[
  [1,   3,  5,  7],
  [10, 11, 16, 20],
  [23, 30, 34, 50]
]

Given target = 3, return true.

思路:由于题目的条件是第x+1行的第一个数比第x行最后一个数大,所以可以使用二分查找;如果题目条件只说逐行递增,逐列递增,可以从右上角开始查找,此方法也适用于本题。

代码一:

class Solution {
public:
    bool searchMatrix(vector<vector<int> > &matrix, int target) {
        const int m = matrix.size();
        if(m == 0)  return false;
        const int n = matrix[0].size();
        if(n == 0)  return false;

        int first = 0;
        int last = m * n - 1;

        while(first <= last)
        {
            int middle = (first + last) / 2;
            int val = matrix[middle / n][middle % n];

            if(val == target)
                return true;
            else if(val < target)
                first = middle + 1;
            else
                last = middle - 1;
        }

        return false;
    }
};

代码二:

class Solution {
public:
    bool searchMatrix(vector<vector<int> > &matrix, int target) {
        const int m = matrix.size();
        if(m == 0)  return false;
        const int n = matrix[0].size();
        if(n == 0)  return false;

        int row = 0;
        int column = n - 1;

        while(row < m && column >= 0)
        {
            if(matrix[row][column] == target)
                return true;
            else if(matrix[row][column] > target)
                column--;
            else
                row++;
        }

        return false;
    }
};

【Leetcode】Search a 2D Matrix,布布扣,bubuko.com

时间: 2024-09-30 18:50:42

【Leetcode】Search a 2D Matrix的相关文章

【Leetcode】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. Intege

【Leetcode】Search a 2D Matrix in JAVA

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 (easy)

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

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 Insert Position (2 solutions)

Search Insert Position Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples.[1,3,5,

【LeetCode】- Search Insert Position(查找插入的位置)

[ 问题: ] Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. 翻译:给你一个排好序的数组和一个目标值,请找出目标值可以插入数组的位置. [ 分析: ]

[LeetCode] 240. Search a 2D Matrix II 搜索一个二维矩阵 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][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][JavaScript]Search a 2D Matrix

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 last integer