[leetcode] 187. Repeated DNA Sequences 解题报告

题目链接: https://leetcode.com/problems/repeated-dna-sequences/

All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.

Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule.

For example,

Given s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT",

Return:
["AAAAACCCCC", "CCCCCAAAAA"].


思路: 一个简单的hash表就可以搞定, 居然这么简单就过了, 也有点太水了吧.

代码如下: 

class Solution {
public:
    vector<string> findRepeatedDnaSequences(string s) {
        if(s.size() < 10) return {};
        for(int i =0; i<= s.size()-10; i++)
        {
            string str = s.substr(i, 10);
            hash[str]++;
            if(hash[str] == 2)
                result.push_back(str);
        }
        return result;
    }
private:
    unordered_map<string, int> hash;
    vector<string> result;
};


你可能感兴趣的:(LeetCode,hash)