Leedcode187.重复的DNA序列
发布时间
阅读量:
阅读量
Leedcode187.重复的DNA序列[online study]
- 题目描述
-
- 代码
题目描述
引用文本中的 DNA 是由多种不同类型的核苷酸组成的集合。例如,在DNA序列中常见的碱基缩写包括 'A' 表示腺嘌呤、'C' 表示胞嘧啶、'G' 表示鸟嘌呤和'T'表示胸腺嘧啶(如 "ACGAATTCCG" 这样的序列)。在DNA研究中发现,在分析DNA序列时识别出重复序列往往能为研究工作提供有益的信息。
设计一个算法用于检测所有长度固定的子串集合,在给定的DNA字符串s中这些特定的子串至少出现两次。
起源于力扣平台(LeetCode)
代码
// An highlighted block
static int x = []() {
std::ios::sync_with_stdio(false);
cin.tie(0);
return 0;
}();//提速
vector<string>findRepeatedDnaSequences(string s)
{
if (s.size() < 10)return {};
unordered_set<string>res, mem;
for (int i = 0; i < s.size() - 9; ++i) {
string cur = s.substr(i, 10);//复制字符串在i位置
if (mem.count(cur)) res.insert(cur);
else mem.insert(cur);
}
for (auto iter = res.begin(); iter != res.end(); ++iter)
{
//输出*iter才是输出那些字符串
cout << *iter << endl;
}
return vector<string>(res.begin(),res.end());
}
/*
使用前要先#include<unordered_set>
声明变量unordered_set <elemType> myset
开始:myset.begin(),结束:myset.end()
插入:myset.insert()
查询位置:myset.find()
删除:myset.erase()
对特定元素进行基数:myset.count(),如果存在就返回1,否则返回0
*/
int main()
{
//std::cout << "Hello World!\n";
string s;
cin >> s;
findRepeatedDnaSequences(s);
}
全部评论 (0)
还没有任何评论哟~
