Search a 2D Matrix II

Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence of it.

This matrix has the following properties:

  • Integers in each row are sorted from left to right.
  • Integers in each column are sorted from up to bottom.
  • No duplicate integers in each row or column.

Example

Consider the following matrix:

[
  [1, 3, 5, 7],
  [2, 4, 7, 8],
  [3, 5, 9, 10]
]

Given target = 3, return 2.

Challenge

O(m+n) time and O(1) extra space

做法:

从每一行的最后一个开始比较,如果比target大,则去掉这一列。如果比target小,则去掉这一行。

public class Solution {
    /**
     * @param matrix: A list of lists of integers
     * @param: A number you want to search in the matrix
     * @return: An integer indicate the occurrence of target in the given matrix
     */
    public int searchMatrix(int[][] matrix, int target) {
        // write your code here
        if(matrix==null || matrix.length==0 || matrix[0].length==0) return 0;
        int count=0;
        int m=matrix.length;
        int n=matrix[0].length;

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

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

        return count;
    }
}
时间: 2024-10-13 12:31:06

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】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

Search a 2D Matrix II【原创】

Search a 2D Matrix II: python学了还么两周可能有些地方写的很新手大家不要笑我 解题思路: 首先当数组为1维数组时if in 直接查找即可 当为 m x n 维数组时: matrix为行增列增的有序矩阵 所以对行和列采用折半查找,即可判断出值所在的新的矩阵范围,一步步缩小,若存在则某一次的行折半或者列折半肯定会遍历到,否则不存在于矩阵中 每一行的第一个元素为最小值,每一列最后一个元素为最大值 若此行的第一个元素比target要大,那肯定在行号小于此行的行中,若此列的最后

[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

刷题240. Search a 2D Matrix II

一.题目说明 题目240. Search a 2D Matrix II,从一个m*n的二维矩阵查找一个整数,每一行从左到右递增,每一列从上到下递增. 二.我的解答 先计算矩阵中点matrix[row_mid][col_mid],然后将矩阵分成4个区间: class Solution{ public: bool dfs(vector<vector<int> >& matrix,int target,int start_x, int end_x, int start_y, in

[leedcode 240] 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 搜索一个二维矩阵之二

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

lintcode 中等题:search a 2d matrix II 搜索二维矩阵II

题目 搜索二维矩阵 II 写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每一列的整数从上到下是排序的. 在每一行或每一列中没有重复的整数. 样例 考虑下列矩阵: [     [1, 3, 5, 7],     [2, 4, 7, 8],     [3, 5, 9, 10] ] 给出target = 3,返回 2 挑战 要求O(m+n) 时间复杂度和O(1) 额外空间 解题 直接遍历,时间复杂度是O(MN) public