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

leetcode 187. Repeated DNA Sequences

2016-03-22 23:45 531 查看
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"].


这题很直白,直接计数感觉太简单,会超时,于是想了种复杂的方法,提交,失望,超时。索性就试试最直接的方法,就是下面这个

class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
vector<string>re;
if (s.size()<11)
return re;
map<string, int>count;
for (int i = 0; i <= s.length() - 10; i++)
{
string str = string(s.begin() + i, s.begin() + i + 10);
count[str]++;
}
for (map<string, int>::iterator it = count.begin(); it != count.end(); it++)
if (it->second > 1)
re.push_back(it->first);
return re;
}
};

靠,竟然通过了。。。彻底无语了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: