leetcode.187. Repeated DNA Sequences

来源:互联网 发布:简单的编程心形 编辑:程序博客网 时间:2024/06/06 03:44
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"].

class Solution {public:    vector<string> findRepeatedDnaSequences(string s) {        vector<string> res;        if (s.size() <= 10) return res;        int mask = 0x7ffffff;        unordered_map<int, int> m;        int cur = 0, i = 0;        while (i < 9) {            cur = (cur << 3) | (s[i++] & 7);        }        while (i < s.size()) {            cur = ((cur & mask) << 3) | (s[i++] & 7);            if (m.find(cur) != m.end()) {                if (m[cur] == 1) res.push_back(s.substr(i - 10, 10));                ++m[cur];             } else {                m[cur] = 1;            }        }        return res;    }};

0 0
原创粉丝点击