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,
题意:判断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;
}
};
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;
}
};
相关文章推荐
- UGUI自适应分辨率
- LeetCode17 17. Letter Combinations of a Phone Number My Submissions QuestionEditorial Solution
- UICollectionView 全解
- Android学习笔记十二之Android基础UI三
- BlockingQueue
- easyui-window 关闭事件,只要关闭窗口就会触发
- gradleb编译的finished with non-zero exit value问题
- iOS中文、数字、符号在UILabel上左右对齐
- ISSUE:-fembed-bitcode is not supported on versions of iOS prior to 6.0
- js中return;、return true、return false;区别
- 1Z0-051-DDL-创建同义词和序列
- Codeforces 527D Clique Problem
- vuejs - the component is a fragment instance
- Android studio 中NameValuePair跟BasicNameValuePair 不能正常导包问题
- How to remove null value in json string
- Gradle version 2.10 is required. Current version is 2.2.1. If using the grad
- openfire log4j:ERROR setFile(null,true) call failed.
- 视频录制,照片拍摄,保存等,UIImagePickerController
- LeetCode 304 Range Sum Query 2D - Immutable(范围求和2D - 不可变)
- Dubbo-Fail to decode request due to: RpcInvocation