leetCode 74.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.

思路:此题还是比較简单的。主要是用两个二分法,才对第一列使用,再对特定的行使用。

详细代码例如以下:

public class Solution {
    public boolean searchMatrix(int[][] m, int target) {
        //两个二分搜索
        //先搜索第一列,找到确定的行,然后再搜索行
        int i = 0;
        int j = m.length-1;
        int mid = 0;
        //搜寻第一列
        while(i <= j){
            mid = (i + j)/2;
            if(m[mid][0] == target){
                return true;
            }else if(m[mid][0] < target){
                i = mid + 1;
            }else{
                j = mid - 1;
            }
        }
        if(m[mid][0] > target){
            if(mid == 0){
                return false;
            }
            mid--;//mid-1
        }
        //搜寻mid行
        i = 0;
        j = m[0].length -1;
        int k = mid;
        //搜寻到了返回true
        while(i <= j){
            mid = (i + j)/2;
            if(m[k][mid] == target){
                return true;
            }else if(m[k][mid] < target){
                i = mid + 1;
            }else{
                j = mid - 1;
            }
        }
        //没有搜寻到,返回false
        return false;
    }
}
时间: 2024-10-24 12:26:21

leetCode 74.Search a 2D Matrix(搜索二维矩阵) 解题思路和方法的相关文章

lintcode 容易题:Search a 2D Matrix 搜索二维矩阵

题目: 搜索二维矩阵 写出一个高效的算法来搜索 m × n矩阵中的值. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每行的第一个数大于上一行的最后一个整数. 样例 考虑下列矩阵: [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] 给出 target = 3,返回 true 挑战 O(log(n) + log(m)) 时间复杂度 解题: 1.最简单的方法就是遍历整个矩阵,时间复杂度:O(log(mn)),这个应该等于O(long(

[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] 74. 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 74. 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 74 Search a 2D Matrix(搜索2D矩阵)

翻译 写一个高效算法用于在一个m x n的矩阵中查找一个值. 这个矩阵有如下属性: 每行的整型数都是从左到右排序的. 每行的第一个元素都比上一行的最后一列大. 例如, 考虑如下矩阵: [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] 给定target = 3,返回true. 原文 Write an efficient algorithm that searches for a value in an m x n matrix. This m

leetcode 74 Search a 2D Matrix ----- 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

19.2.13 [LeetCode 74] 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] 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