zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    进程对象的属性或方法详解
    进程理论以及开启子进程的两种方式
    计算机发展史(多道技术)
    基于socketserver实现的并发(tcp和udp)
    基于udp协议的套接字及udp协议粘包问题
    模拟ssh的远程网络传输
    周考题目及答案
    c/s架构搭建
    网络编程基础
    10.16模拟赛(湖南集训)
  • 原文地址:https://www.cnblogs.com/panini/p/6917600.html
Copyright © 2011-2022 走看看