从数组的右上角(左下角)开始查找
左上角同理,就不再赘述了。
public boolean Find(int target, int [][] array) {
int rows = array.length - 1,cols = array[0].length - 1,firstrows = 0;
boolean flag = false;
if(rows<=0 || cols<=0)
System.out.println("数组为空");
else {
while(firstrows <= rows && cols >= 0) {
if(array[firstrows][cols]>target)
--cols;
else if(array[firstrows][cols]<target)
++firstrows;
else {
flag = true;
break;
}
}
}
if(flag==false)
return false;
else
return true;
}
原文:https://www.cnblogs.com/baby-zuji/p/11275795.html