Leetcode 187. Repeated DNA Sequences

Problem

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"].

Solution

最直接的方法就是从头到尾,借助unordered_map, 对每一个可能性对进行查找,看有没有重复的

class Solution {
public:
    vector<string> findRepeatedDnaSequences(string s) {
        const int N = s.size();
        unordered_map<string,int> mp;
        for( int i = 0; i < N - 9; i++){
            string str = s.substr(i, 10);
            mp[str]++;
        }
        vector<string> rst;
        for( auto it = mp.begin(); it != mp.end(); it++){
            if(it->second > 1) rst.push_back(it->first);
        }
        return rst;
    }
};

优化版
用A, C, G, T 都对应一个数字,然后把每一个字符串化作数字
这样 A 对应 0, C对应1, G对应2, T对应3
每两位表示一个字母,一个序列是10个字母,那就是20个数字是一个序列,掩码mask就是 1 << 20 -1; 
每次向左移动两位。

借用位操作的一些技巧。

这里解释的很好  https://leetcode.com/discuss/74330/20-ms-solution-c-with-explanation

class Solution {
    
public:
    vector<string> findRepeatedDnaSequences(string s) {
        if(s.size() < 11) return vector<string>();
        char lib[4];
        lib['A'] = 0; lib['C'] = 1;lib['G'] = 2;lib['T'] = 3;
        
        unordered_map<int,int> mp;
        vector<string> rst;
        
        int curNum = 0, mask = ( 1 << 20 ) - 1;
        for( int i = 0; i < s.size() ; i++) {
            curNum = ((curNum << 2 )&mask) | lib[s[i]];
            
            if(i >= 9){
                
                if(mp[curNum]++ == 1) {
                    rst.push_back(s.substr(i-9, 10));
                }
            }
        }
        return rst;
    }
};


不知道为什么 local compiler 能通过, leetcode 就不能通过。。。。。。。。。。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值