Why memory limit exceeded?


  • -1
    O

    I tested it in VS, it works well, but not in leetcode. why? anyone can help?

    int strSeqHashVal(const string &str){
    int val=0;
    for(int i=0;i<str.length();i++){
    int tmp=0;
    switch(str[i]){
    case 'A': tmp=0; break;
    case 'C': tmp=1; break;
    case 'G': tmp=2; break;
    case 'T': tmp=3; break;
    }
    val=val*4+tmp;
    }
    return val;
    }
    vector<string> findRepeatedDnaSequences(string s) {
    vector<string> result;
    if(s.length()<=10) return result;
    map<int,int>hashCodeFreq;
    map<int,string> codeStrMap;

        for(int i=0;i<=s.length()-10;i++){
            int key=strSeqHashVal(s.substr(i,10));
            hashCodeFreq[key]++;
            if(codeStrMap.find(key)==codeStrMap.end()) codeStrMap[key]=s.substr(i,10);
        }
        
        for(map<int,int>::iterator iter=hashCodeFreq.begin();iter!=hashCodeFreq.end();++iter){
            if(iter->second>=2) result.push_back(codeStrMap[iter->first]);
        }
        return result;
    }

  • 0
    S

    Do not use the hash table with the string key
    use the int key and it will be OK!


Log in to reply
 

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