[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 ...
随机推荐
- python笔记-dict字典的方法2
#!/usr/bin/env python #-*- coding:utf-8 -*- ''' 概述: 使用键值(key-value)存储,具有极快的查找速度 注意:字典是无序的 key的特性: 1. ...
- 文档对象模型 DOM
1 DOM概述 1.1 什么是DOM 文档对象模型 Document Object Model 文档对象模型 是表示和操作 HTML和XML文档内容的基础API 文档对象模型,是W3C组织推荐的处理可 ...
- JavaScriptDate(日期)
如何使用Date()方法获取当日的日期. getFullYear(): 使用getFullYear()获取年份. getTime(): getTime()返回1970年1月1日至今的毫秒数. setF ...
- Java中的接口和抽象类(转)
在面向对象的概念中,我们知道所有的对象都是通过类来描述的,但是并不是所有的类都是用来描绘对象的,如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类. 抽象类往往用来表征我们在对问题 ...
- 描述符应用 -- 让python变成一个强类型的语言
众所周知,python是一门弱类型的语言,变量可以随意赋值成任意类型,但是通过描述符,我们可以把数据变成强类型的. 我们为数据设置数据描述符,因为数据描述的优先级大于实例属性,所以在给数据赋值的时候会 ...
- 2016-2017 ACM-ICPC CHINA-Final
A Gym 101194A Number Theory Problem 7 的二进制是111,2k-1 的二进制是 k 个 1.所以 k 能被 3 整除时 2k-1 才能被 7 整除. #includ ...
- 图论:HDU2544-最短路(最全、最经典的最短路入门及小结)
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- 有关js的一些小问题
忘了从哪里找来抄下来的: js执行顺序问题 1.函数的声明和调用 “定义式”函数声明 function fn1() {......} "赋值式"函数声明 var f=functio ...
- Asp.net自定义控件开发任我行(2)-TagPrefix标签
摘要 前面我们已经做了一个最简单的TextBox的马甲,此篇文章,我们来讲讲自定义控件的标签.大家可能看到了上一篇中拖放进来的代码是 <cc1:TextEdit ID="TextEdi ...
- uploadify 报http 302错误
uploadify 报http 302错误 原因是系统采用Forms认证,服务端加入匿名认证即可 具体配置如下: <location path="Base/Base/Upload&qu ...