题意
二维数组;
每一行都按照从左到右递增的顺序排序;
每一列都按照从上到下递增的顺序排序;
查找某个值是否存在
思路
从右上角开始查找
往下每个数都变大
往左变小
public class Solution {
public boolean Find(int target, int [][] array) {
int rows=array.length,cols=array[0].length;
int moveToMinx=0,moveToMiny=-1,moveToMaxx=1,moveToMaxy=0,x=0,y=cols-1;
while(x>=0&y>=0&&x<rows&&y<cols){
if(target>array[x][y]){
x+=moveToMaxx;
y+=moveToMaxy;
}
else if(target<array[x][y]){
x+=moveToMinx;
y+=moveToMiny;
}
else{
return true;
}
}
return false;
}
}