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

    链接: http://leetcode.com/problems/search-a-2d-matrix/

    5/28/2017

    1ms, 5%

    注意的问题:

    第11行,因为一开始的二分查找只是比较每行第一个元素,所以碰到比target小的时候,start = mid,而比target大的时候, end = mid - 1

    第17行是为了让matrix[end][0]与target判断。或者可以删掉第17行,把第18行改成if (matrix[end][0] <= target)

    第2个循环里就是正常的二分查找了,left = mid + 1, right = mid - 1的正常情况即可。

     1 public class Solution {
     2     public boolean searchMatrix(int[][] matrix, int target) {
     3         if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
     4             return false;
     5         }
     6         int start = 0, end = matrix.length - 1;
     7         while (start + 1 < end) {
     8             int mid = start + (end - start) / 2;
     9             if (matrix[mid][0] == target) return true;
    10             else if (matrix[mid][0] < target) {
    11                 start = mid;
    12             } else {
    13                 end = mid - 1;
    14             }
    15         }
    16         int row;
    17         if (matrix[end][0] == target || matrix[start][0] == target) return true;
    18         if (matrix[end][0] < target) {
    19             row = end;
    20         } else {
    21             row = start;
    22         }
    23         int left = 0, right = matrix[0].length - 1;
    24         while (left + 1 < right) {
    25             int mid = left + (right - left) / 2;
    26             if (matrix[row][mid] == target) return true;
    27             else if (matrix[row][mid] < target) {
    28                 left = mid + 1;
    29             } else {
    30                 right = mid - 1;
    31             }
    32         }
    33         if (matrix[row][left] == target || matrix[row][right] == target) return true;
    34         return false;
    35     }
    36 }

    别人的答案

    把二维看作是一维二分查找,在整个0~m*n-1里面找。一次循环搞定

    https://discuss.leetcode.com/topic/3227/don-t-treat-it-as-a-2d-matrix-just-treat-it-as-a-sorted-list

     1 class Solution {
     2 public:
     3     bool searchMatrix(vector<vector<int> > &matrix, int target) {
     4         int n = matrix.size();
     5         int m = matrix[0].size();
     6         int l = 0, r = m * n - 1;
     7         while (l != r){
     8             int mid = (l + r - 1) >> 1;
     9             if (matrix[mid / m][mid % m] < target)
    10                 l = mid + 1;
    11             else 
    12                 r = mid;
    13         }
    14         return matrix[r / m][r % m] == target;
    15     }
    16 };

    更多讨论

    https://discuss.leetcode.com/category/82/search-a-2d-matrix

  • 相关阅读:
    HTTP请求报文与响应报文
    RTTI (Run-time type information) in C++
    Advanced C++ | Virtual Copy Constructor
    Virtual Destructor
    Advanced C++ | Virtual Constructor
    What happens when more restrictive access is given to a derived class method in C++?
    Can static functions be virtual in C++?
    Virtual functions in derived classes
    Default arguments and virtual function
    Multiple Inheritance in C++
  • 原文地址:https://www.cnblogs.com/panini/p/6917600.html
Copyright © 2020-2023  润新知