有一种简单的方法可以扩展您的原始代码以适应返回一个包含四个值的列表,而且相对容易:
private static List<Long> search(long value, ArrayList<Long> a) {
List<Long> returnList = new ArrayList<>();
if (null == a) {
return null;
}
if (a.isEmpty()) {
return a;
}
if (a.size()==1) {
return a;
}
if(value < a.get(0)) {
returnList.add(a.get(0));
returnList.add(a.get(1));
return returnList;
}
if(value > a.get(a.size()-1)) {
returnList.add(a.get(a.size()-1));
returnList.add(a.get(a.size()-2));
return returnList;
}
int lo = 0;
int hi = a.size() - 1;
while (lo <= hi) {
int mid = (hi + lo) / 2;
if (value < a.get(mid)) {
hi = mid - 1;
} else if (value > a.get(mid)) {
lo = mid + 1;
} else {
if (mid>1) returnList.add(a.get(mid-2));
if (mid>0) returnList.add(a.get(mid-1));
if (mid<=a.size()-1) returnList.add(a.get(mid+1));
if (mid<=a.size()-2) returnList.add(a.get(mid+2));
return returnList;
}
}
if ((a.get(lo) - value) < (value - a.get(hi))) {
if (lo > 0) returnList.add(a.get(lo-1));
returnList.add(a.get(lo));
if (lo<=a.size()-1) returnList.add(a.get(lo+1));
if (lo<=a.size()-2) returnList.add(a.get(lo+2));
} else {
if (lo > 1) returnList.add(a.get(hi-2));
if (lo > 0) returnList.add(a.get(hi-1));
ReturnList.add(a.get(hi));
if (hi<=a.size()-1) returnList.add(a.get(hi+1));
}
return returnList;
}
但是,如果我们要扩展代码,可能是进行一些小的重构以使其更具可读性的最佳时机。
private static List<Long> addNumbersAroundIndex (int index, Long searchedNum, int numOfElements, List<Long> list) {
List<Long> returnList = new ArrayList<Long>();
int before = numOfElements/2;
int after = numOfElements - before;
if (list.get(index)>searchedNum) {
after --;
} else if(list.get(index)<searchedNum) {
before--;
}
for (int i=before; i>0; i--) {
if (index>i+1) returnList.add(list.get(index-i));
}
if (!list.get(index).equals(searchedNum)) {
returnList.add(list.get(index));
}
for (int i=1; i< after+1; i++) {
if (index+i<=list.size()-1) returnList.add(list.get(index+i));
}
return returnList;
}
private static List<Long> search(long value, int numOfElements, ArrayList<Long> a) {
if (null == a) {
return null;
}
if (a.isEmpty()) {
return a;
}
if (a.size()==1) {
return a;
}
if(value < a.get(0)) {
return addNumbersAroundIndex(0, value, numOfElements,a);
}
if(value > a.get(a.size()-1)) {
return addNumbersAroundIndex(a.size()-1, value,numOfElements,a);
}
int lo = 0;
int hi = a.size() - 1;
while (lo <= hi) {
int mid = (hi + lo) / 2;
if (value < a.get(mid)) {
hi = mid - 1;
} else if (value > a.get(mid)) {
lo = mid + 1;
} else {
return addNumbersAroundIndex(mid,value,numOfElements,a);
}
}
if ((a.get(lo) - value) < (value - a.get(hi))) {
return addNumbersAroundIndex(lo,value,numOfElements,a);
} else {
return addNumbersAroundIndex(hi,value,numOfElements,a);
}
}
用你的一些原始列表对其进行测试:
public static void main(String[] args) {
ArrayList<Long> list = new ArrayList(Arrays.asList(101l, 201l, 301l, 401l, 501l, 601l, 701l, 801l, 901l, 1001l));
System.out.println(search(1020l,5,list));
}
[801, 901, 1001]
System.out.println(search(730l,4,list));
[601, 701, 801, 901]
System.out.println(search(601l,5,list));
[401, 501, 701, 801, 901]