Difficult:Medium
題目
Write an efficient algorithm that searches for a value target in an m x n integer matrix 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.
翻譯
編寫一個在 m x n 整數矩陣矩陣中搜索值目標的高效算法。該矩陣具有以下性質:
每行中的整數從左到右排序。
每行的第一個整數大於前一行的最後一個整數。
範例
Example 1
1 | Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3 |
Example 2
1 | Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13 |
解題思路
1.利用二分法求解
Solution
Code 1 :
1 | var searchMatrix = function(matrix, target) { |