LeetCode //C - 240. Search a 2D Matrix II

240. Search a 2D Matrix II

Write an efficient algorithm that searches for a value target in an m x n integer matrix 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.
     
Example 1:

在这里插入图片描述

Input: 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]], target = 5
Output: true

Example 2:

在这里插入图片描述

Input: 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]], target = 20
Output: false

Constraints:
  • m == matrix.length
  • n == matrix[i].length
  • 1 <= n, m <= 300
  • − 1 0 9 < = m a t r i x [ i ] [ j ] < = 1 0 9 -10^9 <= matrix[i][j] <= 10^9 109<=matrix[i][j]<=109
  • All the integers in each row are sorted in ascending order.
  • All the integers in each column are sorted in ascending order.
  • − 1 0 9 < = t a r g e t < = 1 0 9 -10^9 <= target <= 10^9 109<=target<=109

From: LeetCode
Link: 240. Search a 2D Matrix II


Solution:

Ideas:

To search efficiently in such a matrix, you can take advantage of its properties. Start from the top right corner of the matrix:

  1. If the target is greater than the value in the current position, you can move down because all the values in the current row to the left are smaller than the target.
  2. If the target is smaller than the value in the current position, you can move left because all the values in the current column below are larger than the target.
  3. If you find the target, return true.
  4. If you reach the bounds of the matrix (leftmost column or bottom row) without finding the target, the target does not exist in the matrix.
Code:
bool searchMatrix(int** matrix, int matrixSize, int* matrixColSize, int target) {
    int row = 0;
    int col = *matrixColSize - 1;
    
    while (row < matrixSize && col >= 0) {
        if (matrix[row][col] == target) {
            return true;
        } else if (matrix[row][col] > target) {
            col--;
        } else {
            row++;
        }
    }
    
    return false;
}

相关推荐

  1. LeetCode240. Search a 2D Matrix II

    2024-03-17 15:34:02       56 阅读
  2. 【boost_search搜索引擎】2.正排索引和倒排索引

    2024-03-17 15:34:02       37 阅读
  3. 2402d,d变量2

    2024-03-17 15:34:02       55 阅读
  4. Elastic Search

    2024-03-17 15:34:02       56 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-17 15:34:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-17 15:34:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-17 15:34:02       82 阅读
  4. Python语言-面向对象

    2024-03-17 15:34:02       91 阅读

热门阅读

  1. 栈的顺序存储结构的构建(C++)+ 两栈共享空间

    2024-03-17 15:34:02       43 阅读
  2. 用vue实现“图书馆”前台

    2024-03-17 15:34:02       43 阅读
  3. C++的类型转换

    2024-03-17 15:34:02       45 阅读
  4. c语言实现https服务器(纯享版)

    2024-03-17 15:34:02       37 阅读
  5. Mysql挂掉怎么办

    2024-03-17 15:34:02       43 阅读
  6. 九种背包问题(C++)

    2024-03-17 15:34:02       42 阅读
  7. vue-cli-浏览器实现热更新

    2024-03-17 15:34:02       37 阅读
  8. Android笔记:监听侧边音量键

    2024-03-17 15:34:02       37 阅读