环肿在上网# Joke - 肚皮舞运动
T*g
1 楼
比如这段二分查找“
class BinarySearch> {
private T[] data;//要排序的数据
public BinarySearch(T[] data){
this.data = data;
}
public int search(T key){
int low;
int high;
int mid;
if(data == null)
return -1;
low = 0;
high = data.length - 1;
while(low <= high){
mid = (low + high) / 2;
System.out.println("mid " + mid + " mid value:" + data[mid]);///
if(key.compareTo(data[mid]) < 0){
high = mid - 1;
}else if(key.compareTo(data[mid]) > 0){
low = mid + 1;
}else if(key.compareTo(data[mid]) == 0){
return mid;
}
}
return -1;
}
明明可以用>比较,非要用compareTo是不是给面试官一种为何这么吊的感觉?如果是,
我也去装下逼
class BinarySearch
private T[] data;//要排序的数据
public BinarySearch(T[] data){
this.data = data;
}
public int search(T key){
int low;
int high;
int mid;
if(data == null)
return -1;
low = 0;
high = data.length - 1;
while(low <= high){
mid = (low + high) / 2;
System.out.println("mid " + mid + " mid value:" + data[mid]);///
if(key.compareTo(data[mid]) < 0){
high = mid - 1;
}else if(key.compareTo(data[mid]) > 0){
low = mid + 1;
}else if(key.compareTo(data[mid]) == 0){
return mid;
}
}
return -1;
}
明明可以用>比较,非要用compareTo是不是给面试官一种为何这么吊的感觉?如果是,
我也去装下逼