187. 重复的DNA序列
1 class Solution 2 { 3 public: 4 vector<string> findRepeatedDnaSequences(string s) 5 { 6 int n = s.size(); 7 vector<string> res; 8 unordered_map<string,int> hash; 9 for(int i = 0;i <= n - 10;i ++) hash[s.substr(i,10)]++; 10 11 for(auto a : hash) 12 { 13 if(a.second > 1) res.push_back(a.first); 14 } 15 return res; 16 } 17 };
1 class Solution 2 { 3 public: 4 vector<string> findRepeatedDnaSequences(string s) 5 { 6 unordered_map<string,int> hash; 7 8 vector<string> res; 9 for(int i = 0;i + 10 <= s.size();i ++) 10 { 11 string str = s.substr(i,10); 12 hash[str]++; 13 if(hash[str] == 2) res.push_back(str); 14 } 15 16 return res; 17 } 18 };
Mamba never out

浙公网安备 33010602011771号