leetcode 187. Repeated DNA Sequences

来源:互联网 发布:java中基本数据类型 编辑:程序博客网 时间:2024/06/06 19:39

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;}};


靠,竟然通过了。。。彻底无语了


0 0
原创粉丝点击