1. 程式人生 > >LeetCode 74. Search a 2D Matrix--二維陣列 每行有序,且當前行的首元素大於上一行的末位元素,判斷是否存在某元素

LeetCode 74. Search a 2D Matrix--二維陣列 每行有序,且當前行的首元素大於上一行的末位元素,判斷是否存在某元素

74. Search a 2D Matrix

Medium

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.

Example 1:

Input:
matrix = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] target = 3 Output: true

Example 2:

Input:
matrix = [
  [1,   3,  5,  7],
  [10, 11, 16, 20],
  [23, 30, 34, 50]
]
target = 13
Output: false
import java.util.Arrays;

/**
 * @Desc
 * @Author liuyazhou
 * @CreateTime 2018/11/21 21:29
 **/
public class LeetCode_74_Searcha2DMatrix {

    public static void main(String[] args) {
//        int[] c0Arr = new int[]{1, 3, 4, 7};
//        int location = Arrays.binarySearch(c0Arr, 1);//0;
//        int location = Arrays.binarySearch(c0Arr, 2);//-1-1 = -2
//        int location = Arrays.binarySearch(c0Arr, 5);//-3-1 = -4
//        System.out.println(-(location + 1 + 1));//2,在4之後,7之前

        LeetCode_74_Searcha2DMatrix leetCode_74_searcha2DMatrix = new LeetCode_74_Searcha2DMatrix();
//        int[][] matrix = new int[][]{{1, 3, 5, 7}, {8, 12, 14, 16}, {19, 23, 25, 32}};
        int[][] matrix = new int[][]{{}};
        int target = 7;
        System.out.println(leetCode_74_searcha2DMatrix.searchMatrix(matrix, target));
    }

    /**
     * 先二分法遍歷第一列,如果target存在,則返回true,反之能返回下標,則該target可能在該行,
     * 然後二分法遍歷該行,如果存在則返回下標,大於等於0,則返回true,反之返回[-插入的位置-1],則是負數,則返回false
     *
     * @param matrix
     * @param target
     * @return
     */
    public boolean searchMatrix(int[][] matrix, int target) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0)
            return false;

        int rows = matrix.length;
        int columns = matrix[0].length;
        if (target < matrix[0][0] || target > matrix[rows - 1][columns - 1]) {
            return false;
        }
        if (target == matrix[0][0] || target == matrix[rows - 1][columns - 1]) {
            return true;
        }

        int[] arr = new int[rows];
        for (int i = 0; i < rows; i++) {
            arr[i] = matrix[i][0];
        }

        int location = Arrays.binarySearch(arr, target);
        if (location >= 0) { //說明找到,該元素在第一列某個位置
            return true;
        } else {
            //location <0 ,說明未找到,該元素不在第一列某個位置
            location = -(location + 1 + 1);//該元素就在這一行
        }

        arr = null;//清空該陣列,後續使用
        arr = new int[columns];
        for (int i = 0; i < columns; i++) {
            arr[i] = matrix[location][i];
        }

        location = Arrays.binarySearch(arr, target);
        if (location >= 0) { //說明找到,該元素在該行某個位置
            return true;
        } else {
            //location <0 ,說明未找到,該元素不在該行某個位置
            return false;
        }
    }

}

Runtime: 6 ms, faster than 84.75% of Java online submissions for Search a 2D Matrix.