mirror of
https://github.com/CyC2018/Interview-Notebook.git
synced 2026-10-31 11:05:36 +00:00
Update 算法 - 符号表.md
二分查找符号表在数组已满时扩容。
This commit is contained in:
+8
-1
@@ -216,7 +216,9 @@ public class BinarySearchOrderedST<Key extends Comparable<Key>, Value> implement
|
||||
values[index] = value;
|
||||
return;
|
||||
}
|
||||
// 否则在数组中插入新的节点,需要先将插入位置之后的元素都向后移动一个位置
|
||||
// 否则在数组中插入新的节点:数组已满时先扩容为原来的两倍,再将插入位置之后的元素都向后移动一个位置
|
||||
if (N == keys.length)
|
||||
resize(Math.max(1, 2 * keys.length));
|
||||
for (int j = N; j > index; j--) {
|
||||
keys[j] = keys[j - 1];
|
||||
values[j] = values[j - 1];
|
||||
@@ -226,6 +228,11 @@ public class BinarySearchOrderedST<Key extends Comparable<Key>, Value> implement
|
||||
N++;
|
||||
}
|
||||
|
||||
private void resize(int capacity) {
|
||||
keys = Arrays.copyOf(keys, capacity);
|
||||
values = Arrays.copyOf(values, capacity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Value get(Key key) {
|
||||
int index = rank(key);
|
||||
|
||||
Reference in New Issue
Block a user