[LeetCode] Repeated DNA Sequences hash map
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"].
C++ 标准模板库不常用就容易忘,这个就是用hash map 做一个大表统计的,但是直接unordered_map<string, int > 这样会爆内存。
class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
unordered_map<string,int > mp;
int len = s.length(),curIdx = ;
string curStr;
vector<string >ret;
while(curIdx + <=len){
curStr = s.substr(curIdx,);
if(mp.find(curStr)!=mp.end()){
ret.push_back(curStr);
}
else
mp[curStr] = ;
curIdx ++;
}
return ret;
}
};
处理方法是 可以是将其改为 unordered_map<int ,int >,通过 4进制的转换。另外更可以通过 bitset 再次降低内存,最后需要考虑重复问题,如果用 unordered_map 可以直接标记时候已经添加到返回vector 中了, 用 bitset 可以通过 临时变量 set<string> 存储,最后生成返回的 vector。
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <bitset>
#include <set>
using namespace std; //class Solution {
//public:
// vector<string> findRepeatedDnaSequences(string s) {
// unordered_map<string,int > mp;
// int len = s.length(),curIdx = 0;
// string curStr;
// vector<string >ret;
// while(curIdx + 10<=len){
// curStr = s.substr(curIdx,10);
// if(mp.find(curStr)!=mp.end()){
// ret.push_back(curStr);
// }
// else
// mp[curStr] = 1;
// curIdx ++;
// }
// return ret;
// }
//}; class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
bitset<> bst;
bst.reset();
set<string > ret;
int sum=;
for(int i =;i<;i++)
sum = sum* + helpFun(s[i]);
bst.set(sum);
for( int i=;i<s.length();i++){
sum%=;
sum = sum* + helpFun(s[i]);
if(bst[sum])
ret.insert(s.substr(i-,));
else
bst.set(sum);
}
return vector<string>(ret.begin(),ret.end());
} int helpFun(char c)
{
switch(c){
case 'A': return ;
case 'C': return ;
case 'G': return ;
case 'T': return ;
}
}
}; int main()
{
string s= "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT";
Solution sol;
vector<string > ret = sol.findRepeatedDnaSequences(s);
for(int i=;i<ret.size();i++)
cout<<ret[i]<<endl;
return ;
}
[LeetCode] Repeated DNA Sequences hash map的更多相关文章
- Leetcode OJ : Repeated DNA Sequences hash python solution
Total Accepted: 3790 Total Submissions: 21072 All DNA is composed of a series of nucleotides abb ...
- [LeetCode] Repeated DNA Sequences 求重复的DNA序列
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- [Leetcode] Repeated DNA Sequences
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- LeetCode() Repeated DNA Sequences 看的非常的过瘾!
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- LeetCode 187. 重复的DNA序列(Repeated DNA Sequences)
187. 重复的DNA序列 187. Repeated DNA Sequences 题目描述 All DNA is composed of a series of nucleotides abbrev ...
- lc面试准备:Repeated DNA Sequences
1 题目 All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &quo ...
- Leetcode:Repeated DNA Sequences详细题解
题目 All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: " ...
- 【LeetCode】Repeated DNA Sequences 解题报告
[题目] All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &quo ...
- 【leetcode】Repeated DNA Sequences(middle)★
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
随机推荐
- DevOps - 部署系统 - Cobbler
Cobbler简介 Cobbler由python语言开发,是对PXE和Kickstart以及DHCP的封装.融合很多特性,提供了CLI和Web的管理形式.更加方便的实行网络安装.适用场景:需要大批量的 ...
- 5458. 【NOIP2017提高A组冲刺11.7】质数
5458. [NOIP2017提高A组冲刺11.7]质数 (File IO): input:prime.in output:prime.out Time Limits: 1000 ms Memory ...
- MongDB之各种新增操作
接口IMongDaoCreate: package com.net.test.mongdb.dao; import java.util.List; import com.net.test.mongdb ...
- day 59 MySQL之锁、事务、优化、OLAP、OLTP
MySQL之锁.事务.优化.OLAP.OLTP 本节目录 一 锁的分类及特性 二 表级锁定(MyISAM举例) 三 行级锁定 四 查看死锁.解除锁 五 事务 六 慢日志.执行计划.sql优化 七 ...
- 理解 Objective-c "属性"
理解 Objective-c "属性" @property 是OC中能够快速定义一个属性的关键字,如下我们定义一个属性. @property NSString *String; 这 ...
- Building a Space Station POJ - 2031
Building a Space Station POJ - 2031 You are a member of the space station engineering team, and are ...
- Codeforces Round #459 (Div. 2)-A. Eleven
A. Eleven time limit per test1 second memory limit per test256 megabytes Problem Description Eleven ...
- [译]Exactly once is NOT exactly the same
近日学习Pulsar文档时,注意到Pulsar提到其提供的是effectively-once语义,而不是其它流计算引擎announce的exactly-once语义,并引用了Exactly once ...
- java.util.ArrayList与java.util.Arrays$ArrayList区别
本博客转载自:https://blog.csdn.net/maywehe/article/details/52553954 写demo的时候,为了避免用list.add方法,特意写了个数组然后转换成l ...
- hdu3374 String Problem 最小最大表示法 最小循环节出现次数
#include <iostream> #include <cstring> #include <cstdio> using namespace std; int ...