剑指offer第一题
题目:
在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
public static boolean Solation(int target,int[][] array){
/*
以右上角的数字为指标
*/
int row=0; //右上角行数
int col=array[0].length-1; //右上角的列数
while(col>=0&&row<=array[0].length-1){
System.out.println("row:"+row+"col:"+col);
if(target<array[row][col]){
System.out.println(array[row][col]);
col--;
}else if(target>array[row][col]){
System.out.println(array[row][col]);
row++;
}else{
System.out.println(array[row][col]);
return true;
}
}
return false;
}