jz001

多维有序数组寻找目标值

public class dwsz {
public static void main(String[] args) {
int [][]arry = {{1,2,3},{4,6,9},{10,12,16},{20,32,46}};
test01(66,arry);
}


public static void test01(int taeget,int[][]arry){
if (arry.length==0){
return;
}
int hang = arry.length;
for (int i=0;i<hang;i++){
int x = twoFen(arry[i],0,arry[0].length-1,taeget);
if (x>0){
System.out.println(i);
System.out.println(x);
break;
}
}
}
// 二分法
public static int twoFen(int[]arry,int start,int end,int target){
if (start>=end||arry.length==0){
return -1;
}
while (start<end){
int middle = end-start/2;
if (arry[middle]==target){
return middle;
}else if (arry[middle]>target){
end=middle-1;
}else {
start=middle+1;
}
}
return -1;
}
}

jz001


上一篇:Swift学习之十一:数组与字典(Array and Dictionary are collection types)


下一篇:子类的构造函数与析构函数