题目:给一个行列都排好序的矩阵,找到key的所在位置
public static void main(String[] args) {
int [][]arr={
{1,3,5,6},
{2,4,7,8},
{5,6,9,10},
{9,11,12,13}
};
int []a=go(arr,11);
System.out.println(a[0]+" "+a[1]);
}
public static int [] go(int [][]arr,int key){//从右上开始或左下开始
int i=0;
int j=arr.length-1;
int temp=arr[i][j];
while (temp!=key&&i<arr[0].length&& j>=0){
if(temp>key){
j--;
}else{
i++;
}
temp=arr[i][j];
}
return new int[]{i,j};
}