力扣-187题 重复的DNA序列(C++)- 找指定长度的目标子串+哈希
题目链接:https://leetcode-cn.com/problems/repeated-dna-sequences/
题目如下:
class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
vector<string> result;
int length=s.size();
if(length<10) return result;
unordered_map<string,int> umap;
for(int i=0;i<=length-10;++i){
//注意点:i<=length-10
string tmp=s.substr(i,10);
umap[tmp]++;
}
for(auto e:umap){
if(e.second>1) result.push_back(e.first);
}
return result;
}
};
还没有评论,来说两句吧...