题解 | #二维数组中的查找#
二维数组中的查找
http://www.nowcoder.com/practice/abc3fe2ce8e146608e868a70efebf62e
每行采用二分查找,遍历一遍二维数组,时间复杂度nlogm
public class Solution {
public boolean Find(int target, int [][] array) {
if(array.length < 1 || array[0].length < 1)
return false;
for(int[] arr:array){
int left = 0;
int right = arr.length-1;
while(left <= right){
int mid = (right-left)/2+left;
if(arr[mid] == target)
return true;
else if(arr[mid] < target){
left = mid+1;
}else
right = mid-1;
}
}
return false;
}
}