修复Solution17.java中的问题并添加单元测试-夏洪亮-2023110905 #418
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.
学号:2023110905
修改思路:
原代码的核心思路是使用滑动窗口和位运算来高效检测重复的DNA序列,但存在多处语法错误和一处逻辑问题需要修正。首先需要修复基本的语法错误:将错误的数组声明
List<String> ans[]改为正确的List<String> ans = new ArrayList<>(),修正字符串长度调用为s.length(),修复循环中的===比较运算符为==,并改正位运算表达式中的大括号为小括号。更重要的是逻辑优化,原代码在滑动窗口循环中存在重复添加序列的风险,因为当同一个哈希值在不同位置达到计数2时都会添加,但实际上应该确保每个重复序列只被添加到结果列表一次。此外,还需要完善边界条件处理,确保在字符串长度刚好为10时正确返回空列表,并添加必要的掩码计算来保证20位整数的正确范围。通过这些修改,代码将能够正确实现检测所有重复出现的10字符DNA序列的功能,同时保持O(n)时间复杂度和高效的空间利用率。修改内容: