leetcode之Repeated DNA Sequences

来源:互联网 发布:wince同步软件 win7 编辑:程序博客网 时间:2024/05/16 02:03

题目:

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,


解答:
用bit来进行统计
class Solution {public:    vector<string> findRepeatedDnaSequences(string s) {        bitset<1 << 20> s1;        bitset<1 << 20> s2;        vector<string> res;        map<char,int> charToVal;        charToVal.insert(make_pair('A',0));        charToVal.insert(make_pair('T',1));        charToVal.insert(make_pair('C',2));        charToVal.insert(make_pair('G',3));        int len = s.length();        if(len <= 10)            return res;        int val = 0;        int i;        for(i = 0; i < 10;++i)            val = (val << 2) | charToVal[s[i]];        s1.set(val);        int mask = (1 << 20) - 1;        for(i = 10;i < len;++i)        {            val = ((val << 2) & mask) | charToVal[s[i]];            if(s2[val])                continue;            if(s1[val])            {                res.push_back(s.substr(i - 9, 10));                s2.set(val);            }            else                s1.set(val);                    }        return res;    }};

0 0
原创粉丝点击