准确来讲,字符串哈希直接使用 C++ 内置的 hash_map 就可以了,不需要自己计算

class Solution {
const int L =10;
public:
vector findRepeatedDnaSequences(string s) {
// 字符串哈希
vector ans;
unordered_map<string, int> cnt;
int lenS = s.length();

    for(int i=0;i<=lenS-L;++i){
        string subS = s.substr(i,L);
        if(++cnt\[subS\] == 2){
            ans.push\_back(subS);
        }
    }

    return ans;
}

};

編集日

*~( ̄▽ ̄)~[お茶]を一杯ください

Solvarg WeChat 支払う

WeChat 支払う

Solvarg Alipay

Alipay

Solvarg PayPal

PayPal