题目:
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.
Example:
Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT" Output: ["AAAAACCCCC", "CCCCCAAAAA"]
代码:
方法一——map:
class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
int len = s.length();
map<string, int> m;
vector<string> res;
for (int i = 0; i < len - 9; i++) {
string t = s.substr(i, 10);
if (!m.count(t)) {
m[t] = 1;
}
else {
if(find(res.begin(),res.end(),t)==res.end())
res.push_back(t);
}
}
return res;
}
};
方法二——位操作:
class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
unordered_set<int> seen;
unordered_set<int> dup;
vector<string> result;
vector<char> m(26);
m['A' - 'A'] = 0;
m['C' - 'A'] = 1;
m['G' - 'A'] = 2;
m['T' - 'A'] = 3;
for (int i = 0; i + 10 <= s.size(); ++i) {
string substr = s.substr(i, 10);
int v = 0;
for (int j = i; j < i + 10; ++j) { //20 bits < 32 bit int
v <<= 2;
v |= m[s[j] - 'A'];
}
if (seen.count(v) == 0) { //not seen
seen.insert(v);
} else if (dup.count(v) == 0) { //seen but not dup
dup.insert(v);
result.push_back(substr);
} //dup
}
return result;
}
};
想法:
多看别人做的
来源:https://blog.csdn.net/qq_35455503/article/details/101435946