zoukankan      html  css  js  c++  java
  • 28. Search a 2D Matrix 【easy】

    28. Search a 2D Matrix 【easy】

    Write an efficient algorithm that searches for a value in an mn 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.
    Example

    Consider the following matrix:

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

    Given target = 3, return true.

    Challenge 

    O(log(n) + log(m)) time

    解法一:

     1 class Solution {
     2 public:
     3     /*
     4      * @param matrix: matrix, a list of lists of integers
     5      * @param target: An integer
     6      * @return: a boolean, indicate whether matrix contains target
     7      */
     8     bool searchMatrix(vector<vector<int>> &matrix, int target) {
     9         int row = matrix.size();
    10         if (row == 0) {
    11             return false;
    12         }
    13         
    14         int col = matrix[0].size();
    15         if (col == 0) {
    16             return false;
    17         }
    18         
    19         int start = 0;
    20         int end = row * col - 1;
    21         
    22         while (start + 1 < end) {
    23             int mid = start + (end - start) / 2;
    24             
    25             if (matrix[mid / col][mid % col] == target) {
    26                 return true;
    27             }
    28             else if (matrix[mid / col][mid % col] < target) {
    29                 start = mid;
    30             }
    31             else if (matrix[mid / col][mid % col] > target) {
    32                 end = mid;
    33             }
    34         }
    35         
    36         if (matrix[start / col][start % col] == target 
    37             || matrix[end / col][end % col] == target) {
    38             return true;
    39         }
    40         else {
    41             return false;
    42         }
    43     }
    44 };

    注意:

    1、边界值合法性检查

    2、二维数组转一维数组的计算方式

    解法二:

     1 public class Solution {
     2     public boolean searchMatrix(int[][] matrix, int target) {
     3         if (matrix == null || matrix.length == 0) {
     4             return false;
     5         }
     6         if (matrix[0] == null || matrix[0].length == 0) {
     7             return false;
     8         }
     9         
    10         int row = matrix.length;
    11         int column = matrix[0].length;
    12         
    13         // find the row index, the last number <= target 
    14         int start = 0, end = row - 1;
    15         while (start + 1 < end) {
    16             int mid = start + (end - start) / 2;
    17             if (matrix[mid][0] == target) {
    18                 return true;
    19             } else if (matrix[mid][0] < target) {
    20                 start = mid;
    21             } else {
    22                 end = mid;
    23             }
    24         }
    25         if (matrix[end][0] <= target) {
    26             row = end;
    27         } else if (matrix[start][0] <= target) {
    28             row = start;
    29         } else {
    30             return false;
    31         }
    32         
    33         // find the column index, the number equal to target
    34         start = 0;
    35         end = column - 1;
    36         while (start + 1 < end) {
    37             int mid = start + (end - start) / 2;
    38             if (matrix[row][mid] == target) {
    39                 return true;
    40             } else if (matrix[row][mid] < target) {
    41                 start = mid;
    42             } else {
    43                 end = mid;
    44             }
    45         }
    46         if (matrix[row][start] == target) {
    47             return true;
    48         } else if (matrix[row][end] == target) {
    49             return true;
    50         }
    51         return false;
    52     }
    53 }

    两次二分,值得借鉴!

  • 相关阅读:
    (Java实现) 洛谷 P1603 斯诺登的密码
    (Java实现) 洛谷 P1036 选数
    (Java实现) 洛谷 P1036 选数
    (Java实现) 洛谷 P1012 拼数
    (Java实现) 洛谷 P1012 拼数
    (Java实现) 洛谷 P1028 数的计算
    (Java实现) 洛谷 P1028 数的计算
    (Java实现) 洛谷 P1553 数字反转(升级版)
    8.4 确定两个日期之间的月份数或年数
    (Java实现) 洛谷 P1553 数字反转(升级版)
  • 原文地址:https://www.cnblogs.com/abc-begin/p/7543687.html
Copyright © 2011-2022 走看看