博客主页:http://blog.csdn.net/minna_d
题目:
给一个n个元素的线性表A,对于每个数Ai,找到它之前的数中,和它最接近的数。即对于每个i,计算
Ci = min{|Ai-Aj| | 1<=j<i} 规定C1 = 0。
其实就是给定一个数组, 在a[0....i-1]中求离a[i]最近的值, 其实这里有个bug,那就是,如果对与6而言5,7都离它一样, 那么该输出谁呢
N久不写C, 感觉怪怪的, 写了一个java版。
思路:
用一个临时数组存储,离a[i]最近值
用另外一个数组存储前a[0, i-1]的排序值
这样一个好处就就是能在result[i-1]的基础之上计算result[i]的结果,
查询时间复杂度为lgn,插入时间复杂度为1(因为Arrays.copy中调用System.arraycopy的缘故)
public static void main(String[] args) {
List<Integer> list = Lists.newArrayList(1, 8, 6, 6, 7, 5, 4, 1, 0, 8);
Integer[] result = new Integer[list.size()];
List<Integer> tmp = Lists.newArrayList(list.get(0));
result[0] = 0;
for (int i = 1; i < list.size(); i++) {
Integer willBeSort = list.get(i);
Integer shouldInsert = Collections.binarySearch(tmp, willBeSort);
// 该值已经存在
if (shouldInsert >= 0) {
result[i] = willBeSort;
tmp.add(shouldInsert, willBeSort);
continue;
}
shouldInsert = Math.abs(shouldInsert + 1);
// 在最后位置插入
if (shouldInsert == tmp.size()) {
result[i] = list.get(shouldInsert - 1);
tmp.add(shouldInsert, willBeSort);
continue;
}
// 在最前位置插入
if (shouldInsert == 0) {
result[i] = list.get(0);
tmp.add(shouldInsert, willBeSort);
continue;
}
// 中间位置插入
int b = tmp.get(shouldInsert);
int a = tmp.get(shouldInsert - 1);
result[i] = Math.abs(a - willBeSort) > Math.abs(b - willBeSort) ? b : a;
tmp.add(shouldInsert, willBeSort);
}
System.out.println(Joiner.on(",").join(result));
}
输出结果:
0,1,8,6,6,6,5,1,1,8