我的LeetCode代码仓:https://github.com/617076674/LeetCode
原题链接:https://leetcode-cn.com/problems/repeated-dna-sequences/description/
题目描述:
知识点:哈希表
思路:用哈希表记录每个10个字母长的序列出现的次数
时间复杂度和空间复杂度均是O(n),其中n为字符串s的长度。
JAVA代码:
public class Solution {
public List<String> findRepeatedDnaSequences(String s) {
HashMap<String, Integer> hashMap = new HashMap<>();
for(int i = 0; i < s.length() - 9; i++){
String string = s.substring(i, i + 10);
if(hashMap.containsKey(string)){
hashMap.put(string, hashMap.get(string) + 1);
}else{
hashMap.put(string, 1);
}
}
List<String> list = new ArrayList<>();
for(String string : hashMap.keySet()){
if(hashMap.get(string) > 1){
list.add(string);
}
}
return list;
}
}
LeetCode解题报告: