My C++ solution


  • -5
    Z

    {
    class Solution {
    public:
    vector<string> findRepeatedDnaSequences(string s) {
    int char2num[300]={0};
    char2num['A']=0;
    char2num['G']=1;
    char2num['C']=2;
    char2num['T']=3;
    char num2char[4]={'A','G','C','T'};
    map<long long,int> a;
    for (int i=0;i<s.size();i++){
    if (i+10>s.size()){
    break;
    }
    long long x=0;
    for (int j=9;j>=0;j--){
    x=(x<<2)+char2num[s[i+j]];
    }
    ++a[x];
    }
    vector<string> res;
    for (auto i:a){
    if (i.second>1){
    long long x=i.first;
    string t;
    for (int j=0;j<10;j++){
    t.push_back(num2char[x&3]);
    x>>=2;
    }
    res.push_back(t);
    }
    }
    return res;
    }
    };
    }


  • 0
    P

    Please put your code using {}, its not readable other wise


  • -1
    A

    Readability is too poor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        vector<string> findRepeatedDnaSequences_set(string s) {
                if(s.size() <= 10) return vector<string>();
                unordered_set<string> result;
                unordered_set<string> hashSet;
                for(size_t i = 0; i < s.size() - 10; ++i) {
                    string temp = s.substr(i, 10);
                    if(hashSet.find(temp) != hashSet.end()) {
                        result.insert(temp);
                    } else {
                        hashSet.insert(temp);
                    }
                }
    
                return vector<string>(result.begin(), result.end());
         }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.