From 8b057da93ec2e46758c1555e884d924f64e16e2d Mon Sep 17 00:00:00 2001 From: CyC2018 Date: Sat, 31 Oct 2026 14:03:54 +0800 Subject: [PATCH] =?UTF-8?q?Update=20=E7=AE=97=E6=B3=95=20-=20=E7=AC=A6?= =?UTF-8?q?=E5=8F=B7=E8=A1=A8.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 二分查找符号表在数组已满时扩容。 --- notes/算法 - 符号表.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/notes/算法 - 符号表.md b/notes/算法 - 符号表.md index 426e94ef..68e2c9d2 100644 --- a/notes/算法 - 符号表.md +++ b/notes/算法 - 符号表.md @@ -216,7 +216,9 @@ public class BinarySearchOrderedST, 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, 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);