修复Solution7的错误,并添加测试类。学号:2023120245 #374
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
学号:2023120245
修正并查集大小:将并查集初始化的大小从 len - 1 改为 len,避免索引越界。
补全循环括号:为字符分组的 for 循环添加花括号,确保循环体包含所有必要的代码。
for (int i = 0; i < len; i++) {
int root = unionFind.find(i);
hashMap.computeIfAbsent(root, key -> new PriorityQueue<>()).offer(charArray[i]);
}
直接修改字符数组:在重组字符串时,直接修改原始字符数组,去除 StringBuilder 和多余的空格添加。
for (int i = 0; i < len; i++) {
int root = unionFind.find(i);
charArray[i] = hashMap.get(root).poll();
}
调整条件判断:将条件从 pairs.size() <= 1 改为 pairs.isEmpty(),确保包含单个索引对的情况被正确处理。
if (pairs.isEmpty()) {
return s;
}