Leetcode 187. repetitive DNA sequences in C ++ and solving ideas

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/gjh13/article/details/90733161

Method One: Use unordered_map, violence Solution

Problem-solving ideas:

Use unordered_map substring <string, int> Traversal string s, a length of each substring count 10, then traverse the unordered_map once, the count is greater than 1 is added to the result in 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;
    }
};

 

 

Guess you like

Origin blog.csdn.net/gjh13/article/details/90733161