文章标题

来源:互联网 发布:2017怎么开手机淘宝店 编辑:程序博客网 时间:2024/06/06 14:09

【题目】
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;    }};
0 0
原创粉丝点击