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/

    题解:

    可以把矩阵当做一个长的行向量进行binary search, 也可以对行列分别进行两次binary search,都差不多。

    Time Complexity - O(log(mn)),  Space Complexity - O(1).

    public class Solution {
        public boolean searchMatrix(int[][] matrix, int target) {
            if(matrix == null || matrix.length == 0)
                return false;
            int rowNum = matrix.length, colNum = matrix[0].length;
            int lo = 0, hi = rowNum * colNum - 1;
            
            while(lo <= hi) {
                int mid = lo + (hi - lo) / 2;
                int row = mid / colNum, col = mid % colNum;
                if(matrix[row][col] < target)
                    lo = mid + 1;                
                else if(matrix[row][col] > target)
                    hi = mid - 1;
                else
                    return true;
            }
            
            return false;
        }
    }

    二刷:

    跟一刷一样,利用二分搜索, 然后在搜索的时候把mid转化为矩阵的行和列坐标就可以了。

    Time Complexity - O(logmn),  Space Complexity - O(1).

    Java:

    public class Solution {
        public boolean searchMatrix(int[][] matrix, int target) {
            if (matrix == null || matrix.length == 0) {
                return false;
            }
            int rowNum = matrix.length, colNum = matrix[0].length;
            int lo = 0, hi = rowNum * colNum - 1;
            while (lo <= hi) {
                int mid = lo + (hi - lo) / 2;
                int row = mid / colNum;
                int col = mid % colNum;
                if (matrix[row][col] == target) {
                    return true;
                } else if (matrix[row][col] < target) {
                    lo = mid + 1;
                } else {
                    hi = mid - 1;
                }
            }
            return false;
        }
    }

    三刷:

    把2D Matrix转换为1D Array,然后使用Binary Search就可以了。假设1D Array中的index是mid,转换就是  row = mid / colNum, col = mid % colNum

    Java:

    public class Solution {
        public boolean searchMatrix(int[][] matrix, int target) {
            if (matrix == null || matrix.length == 0) { return false; }
            int rowNum = matrix.length, colNum = matrix[0].length;
            int lo = 0, hi = rowNum * colNum - 1;
            while (lo <= hi) {
                int mid = lo + (hi - lo) / 2;
                int rowMid = mid / colNum;
                int colMid = mid % colNum;
                if (matrix[rowMid][colMid] == target) { return true; }
                else if (matrix[rowMid][colMid] < target) { lo = mid + 1; }
                else { hi = mid - 1; }
            }
            return false;
        }
    }

    测试:

  • 相关阅读:
    SSM添加数据后自动获取ID
    EasyUI分页
    JavaScript增强AJAX基础
    高德地图MapAPI地图展示
    项目json代码
    JavaScript 事件机制
    JavaScript event flow
    java和JavaScript的区别
    history of program
    javaScript obj
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4437116.html
Copyright © 2011-2022 走看看