您的位置:首页 > 编程语言 > C语言/C++

文章标题

2016-06-09 10:59 465 查看
【题目】

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”].

【题意】

统计长度为10且出现超过1次的子串。

【算法】

需要用的【substr】函数

str.substr(startpos, length);

startpos 是起始字符的序号

length 是[从 startpos 开始]取的字符串长度(包括startpos )。

构造hash表。

【code】

class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
vector<string>res;
map<string,int>mp;
map<string,int>::iterator ml;
for (int i = 9; i < s.size(); i++)
{
string stmp = s.substr(i-9,10);
if(mp.count(stmp))
mp[stmp] ++;
else
mp[stmp] = 1;

}
for (ml = mp.begin(); ml!=mp.end(); ml++)
{
if(ml->second>1)
res.push_back(ml->first);
}
return res;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode+ c语言