Leetcode 187. 重复的DNA序列 解题思路及C++实现
生活随笔
收集整理的這篇文章主要介紹了
Leetcode 187. 重复的DNA序列 解题思路及C++实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
方法一:使用unordered_map,暴力解法
解題思路:
使用unordered_map<string, int>遍歷字符串s,將每一個長度為10的子字符串計數,然后再遍歷一次這個unordered_map,將計數值大于1的子字符串添加到結果res中。
class Solution { public:vector<string> findRepeatedDnaSequences(string s) {if(s.length() < 10) return {};vector<string> res;unordered_map<string, int> mp;for(int i = 0; i < s.length() - 9; i++){mp[s.substr(i, 10)]++;}for(auto m: mp){if(m.second > 1)res.push_back(m.first);}return res;} };?
?
總結
以上是生活随笔為你收集整理的Leetcode 187. 重复的DNA序列 解题思路及C++实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leetcode 137. 只出现一次的
- 下一篇: Leetcode 201. 数字范围按位