Leetcode --- Search a 2D Matrix(Medium) === ## Description 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 ![ex](https://i.imgur.com/qr2XXUy.jpg) **Input**: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3 **Output:** ==true== ## Solution 因為每一列都已經被sorted,所以對2D matrix的每一列分別做binary search. ### implement code ```java= import java.util.*; class Solution { public boolean searchMatrix(int[][] matrix, int target) { for(int i =0 ;i<matrix.length;i++) if(Arrays.binarySearch(matrix[i],target) >=0) return true; return false; } } ``` ### Time complexity Matrix size : nxm binary search :O(log~m~) m為行數(一列的size) 要做n次 => ==O(nlog~m~)== ### Submission on Leetcode Runtime: **0 ms**, faster than **100.00%** of Java online submissions for Search a 2D Matrix. Memory Usage: **38.3 MB, less than 63.41%** of Java online submissions for Search a 2D Matrix. ###### tags: `Leetcode` `Medium` `Search`