Leetcode 240. Search a 2D Matrix II [某二维数组从左往右升序,从上往下升序,查找某个值是否存在,存在则返回true,反之返回

305 阅读1分钟

Write an efficient algorithm that searches for a target value in an m x n integer matrix. The matrix has the following properties:

  • Integers in each row are sorted in ascending from left to right.
  • Integers in each column are sorted in ascending from top to bottom.

Example 1:

Input: matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5
Output: true

Example 2:

Input: matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20
Output: false

Constraints:

  • m == matrix.length

  • n == matrix[i].length

  • 1 <= n, m <= 300

  • -109 <= matix[i][j] <= 109

  • All the integers in each row are sorted in ascending order.

  • All the integers in each column are sorted in ascending order.

  • -109 <= target <= 109

    package com.array;

    /**

    • @Author you guess

    • @Date 2021/1/10 21:28

    • @Version 1.0

    • @Desc */ public class Leetcode_240_Searcha2DMatrixII {

      /**

      • 2021/1/10 自己写的 ,方法1

      • 先确定那几行的范围,再二分查找每行

      • Runtime: 8 ms, faster than 24.15% of Java online submissions for Search a 2D Matrix II.

      • Memory Usage: 51.3 MB, less than 8.84% of Java online submissions for Search a 2D Matrix II.

      • @param matrix

      • @param target

      • @return */ public boolean searchMatrix(int[][] matrix, int target) { int rows = matrix.length; int columns = matrix[0].length;

        int topRow = 0; for (int i = rows - 1; i >= 0; i--) { if (matrix[i][columns - 1] < target) { topRow = i; break; } }

        int bottomRow = rows - 1; for (int i = 0; i < rows; i++) { if (matrix[i][0] > target) { bottomRow = i - 1; break; } }

        for (int i = topRow; i <= bottomRow; i++) { if (binarySearch(matrix, target, i)) { return true; } } return false; }//searchMatrix

      public boolean binarySearch(int[][] matrix, int target, int row) {

       int head = 0;
       int tail = matrix[0].length - 1;
      
       while (head <= tail) {
           int mid = (head + tail) / 2;
           if (matrix[row][mid] == target) {
               return true;
           } else if (matrix[row][mid] < target) {
               head = mid + 1;
           } else {
               tail = mid - 1;
           }
       }
       return false;
      

      }

      /**

      • 2020-01-10,方法2

      • 参考【以右上角元素为基准】leetcode.com/problems/se…

      • 以左下角做基准,tar比当前元素大则往右找,tar比当前元素小则往上找!

      • Runtime: 4 ms, faster than 99.94% of Java online submissions for Search a 2D Matrix II.

      • Memory Usage: 44.8 MB, less than 34.46% of Java online submissions for Search a 2D Matrix II.

      • 时间复杂度O(N+M)

      • 这个方法是共用的!!

      • 可解决LeetCode 74. Search a 2D Matrix 【Runtime: 0 ms, faster than 100.00%】

      • @param matrix

      • @param target

      • @return */ public boolean searchMatrix2(int[][] matrix, int target) { int row = matrix.length - 1; int col = 0;

        while (row >= 0 && col < matrix[0].length) { if (matrix[row][col] == target) { return true; } else if (matrix[row][col] > target) { row--;//target比它小,在上边 } else { col++;//target比它大,在右边 } } return false; }

      public static void main(String[] args) { Leetcode_240_Searcha2DMatrixII main = new Leetcode_240_Searcha2DMatrixII(); // System.out.println(main.searchMatrix(new int[][]{{1, 4, 7, 11, 15}, {2, 5, 8, 12, 19}, {3, 6, 9, 16, 22}, {10, 13, 14, 17, 24}, {18, 21, 23, 26, 30}} // , 5));//true

    // System.out.println(main.searchMatrix2(new int[][]{{1, 4, 7, 11, 15}, {2, 5, 8, 12, 19}, {3, 6, 9, 16, 22}, {10, 13, 14, 17, 24}, {18, 21, 23, 26, 30}} // , 14));//true

        System.out.println(main.searchMatrix2(new int[][]{{1, 4, 7, 11, 15}, {2, 5, 8, 12, 19}, {3, 6, 9, 16, 22}, {10, 13, 14, 17, 24}, {18, 21, 23, 26, 30}}
                , 20));//false
    }
    

    }

方法2,是共用的! 可解决LeetCode 74. Search a 2D Matrix 【Runtime: 0 ms, faster than 100.00%】