您的位置:首页 > 产品设计 > UI/UE

187. Repeated DNA Sequences

2016-06-29 17:12 302 查看
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"].

题意:判断DNA序列中出现次数大于等于两次的长度为10的序列。
思路:以长度为10的序列建立hashmap,记录他的出现情况,true是出现一次,false是出现多次。

class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
vector<string> res;
unordered_map<string, bool> mymap;
int start = 0;
string tmp;
unordered_map<string, bool>::iterator it;
while (start + 9 < s.size()){
tmp = s.substr(start, 10);
it = mymap.find(tmp);
if (it != mymap.end()){
if (it->second){
res.push_back(tmp);
it->second = false;
}
}
else{
mymap[tmp] = true;
}
start++;
}
return res;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: