二维数组查找,上下左右四个边界缩小,为啥会超时啊?
二维数组中的查找
http://www.nowcoder.com/questionTerminal/abc3fe2ce8e146608e868a70efebf62e
求个大佬帮我看下为啥会超时,用的是从上下左右四个边界缩,直到只有一个元素的方法。
public class Solution {
public boolean Find(int target, int [][] array) {
if(array.length==0||array[0].length==0)return false;
int rowl=0,rowr=array.length-1;
int coll=0,colr=array[0].length-1;
int temp1;
int temp2;
while(true){
temp1=array[rowl][coll];
temp2=array[rowr][colr];
if(temp1==target)return true;
if(temp1>target||temp2<target)return false;
while(array[rowr][coll]>target&&rowr>rowl)rowr--;
while(array[rowl][colr]<target&&rowl<rowr)rowl++;
while(array[rowr][coll]<target&&coll<colr)coll++;
while(array[rowl][colr]>target&&coll<colr)colr--;
}
}
}
查看6道真题和解析