[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 ...
随机推荐
- mysql中如何不重复插入,mysql 重复的不插入,mysql唯一的插入
INSERT INTO new_schedules_spider_shipsname ( ID,SCAC,VESSEL,VOYAGE,SERVICE_NAME,MD5 ) SELECT NULL,%s ...
- ZendFramework-2.4 源代码 - 开始
ZendFramework 是一种PHP框架. 写在前面 最早遇到ZendFramework是在阅读一款叫Magento电子商务系统源代码时看到,后来因为工作,把注意力侧重在其他方面,就搁置了继续了解 ...
- Python装饰器使用规范案例详解
由于函数也是一个对象,而且函数对象可以被赋值给变量,所以,通过变量也能调用该函数. >>> def now(): ... print('2015-3-25') ... >> ...
- A1025 PAT Ranking (25)(25 分)
A1025 PAT Ranking (25)(25 分) Programming Ability Test (PAT) is organized by the College of Computer ...
- Java多线程-yield(),sleep()以及wait()的区别
从操作系统的角度讲,os会维护一个ready queue(就绪的线程队列).并且在某一时刻cpu只为ready queue中位于队列头部的线程服务.但是当前正在被服务的线程可能觉得cpu的服务质量不够 ...
- 浅谈I/O模型
在学习线程,NIO等知识时都需要知道一些基础知识. 一.什么是同步或异步 同步:个人通俗理解多个人排队打饭一个窗口,只有前面一个人打完了,后面的人才能打.如果前面人因为什么原因一直站在那里不走,后面的 ...
- synchronized同步方法和同步代码块的区别
同步方法默认使用this或者当前类做为锁. 同步代码块可以选择以什么来加锁,比同步方法更精确,我们可以选择只有会在同步发生同步问题的代码加锁,而并不是整个方法. 同步方法使用synchronized修 ...
- Linux QA
gitee: https://gitee.com/dhclly/icedog.script.test/blob/master/doc/linux/linux-qa.md 1. linux 中的 ll( ...
- 【Reverse Integer】cpp
题目: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click ...
- MFC深入浅出读书笔记第一部分
最近看侯捷的MFC深入浅出,简单总结一下. 第一章首先就是先了解一下windows程序设计的基础知识,包括win32程序开发基础,什么*.lib,*.h,*.cpp的,程序入口点WinMain函数,窗 ...