• 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;
        }
    }

    测试:

  • 相关阅读:
    Linux_9/ RAID & LVM
    Linux_8/ fdisk, xfs_quota, edquota
    Linux_7/(chattr, lsattr), (setfacl, getfacl),su
    Linux_6/ Vim, Shell(下),(at, crond), (SUID, SGID, SBIT)
    Linux_5/ Vim, Shell(上)
    Linux_4/ |, (>, 2>, &>, >>, 2>>)
    Map集合的遍历
    List集合三种遍历方法
    MySQL安装
    排序法
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4437116.html
Copyright © 2020-2023  润新知