问题:
使用二分查找算法实现:在一组有序的list中,如何删除其中一个数字。那么,下次则插入该数字。(list存放的数字为:1,2,3,4,5,6。我删除2之后,下次插入的数字还是2。)
String resultStr = “”;
List list = new
int len = list.size();
if(len == 0){
resultStr = “01”;
}else{
for(int i = 1; i <= len; i++){
int flag = 0; //默认i,不存在list中
int left = 0;
int right = list.size() -1;
int middle ;
while(left <= right){
middle = (left+right)/2;
Map map = (Map)list.get(middle);
int equip_num = Integer.parseInt(map.get(“equip_num”).toString());
if(equip_num < i ){
left = middle +1 ;
}else if(equip_num > i){
right = middle – 1;
}else{
flag = 1;
break;
}
}
if(flag == 0){//不存在
if(i < 10){
resultStr = “0”+String.valueOf(i);
}else{
resultStr = String.valueOf(i);
}
break;
}else if(flag == 1){
continue;
}
}
return resultStr;