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"].
Hide Tags

Hash Table Bit Manipulation

 

  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的更多相关文章

  1. Leetcode OJ : Repeated DNA Sequences hash python solution

    Total Accepted: 3790 Total Submissions: 21072     All DNA is composed of a series of nucleotides abb ...

  2. [LeetCode] Repeated DNA Sequences 求重复的DNA序列

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  3. [Leetcode] Repeated DNA Sequences

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  4. LeetCode() Repeated DNA Sequences 看的非常的过瘾!

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  5. LeetCode 187. 重复的DNA序列(Repeated DNA Sequences)

    187. 重复的DNA序列 187. Repeated DNA Sequences 题目描述 All DNA is composed of a series of nucleotides abbrev ...

  6. lc面试准备:Repeated DNA Sequences

    1 题目 All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &quo ...

  7. Leetcode:Repeated DNA Sequences详细题解

    题目 All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: " ...

  8. 【LeetCode】Repeated DNA Sequences 解题报告

    [题目] All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &quo ...

  9. 【leetcode】Repeated DNA Sequences(middle)★

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

随机推荐

  1. list变set去重,set交集

    set 取交集 并集 删除没有的元素  不会报错 remove 会报错 https://www.cnblogs.com/alex3714/articles/5717620.html

  2. goaccess 安装

    今天尝试搭建goaccess,用于分析access.log文件,但安装并不顺利,小记一下自己遇到的问题及解决方法 系统环境:CentOS release 6.9 一.参照官网教程进行搭建 $ wget ...

  3. 科学计算库Numpy——数组生成

    等差数组 使用np.arange()或np.linspace()生成元素是等差数列的数组. 以10为底的数组 使用np.logspace()生成元素是以10为底的数组. 数组扩展 使用np.meshg ...

  4. 内存管理小结(2)--伙伴系统API

    伙伴系统分配内存以2的整数幂次的页数为单位.提供的API主要分为分配类与释放类. 1.分配类 1.1unsigned long __get_free_pages(gfp_t gfp_mask, uns ...

  5. 2 Model层 -定义模型

    1  ORM简介 MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库 ORM是“对象-关系-映射” ...

  6. Gradle task

    本文来自网易云社区 作者:孙有军 1:gradle脚本是使用groovy语言写的(DSL),groovy中有一个重要的概念闭包(Closure),Closure是一段单独的代码块,它可以接收参数,返回 ...

  7. Python+Selenium中级篇之-封装一个自己的类-浏览器引擎类

    前一篇文章我们知道了,如何去封装几个简单的Selenium方法到我们自定义的类,这次我们编写一个类,叫浏览器引擎类,通过更改一个字符串的值,利用if语句去判断和控制启动那个浏览器.这里我们暂时,支持三 ...

  8. Jmeter-深入理解cookie,session,token

    1.很久很久以前,Web 基本上就是文档的浏览而已, 既然是浏览,作为服务器, 不需要记录谁在某一段时间里都浏览了什么文档,每次请求都是一个新的HTTP协议, 就是请求加响应,  尤其是我不用记住是谁 ...

  9. jquery左右滑动菜单

    <div class="mini-container" style="position:relative;height:100%;"> <di ...

  10. github pages+阿里云域名绑定搭建个人博客

    1.选择mast 配置cname 设置域名 同时在github设置里面进行绑定 2.获取github pages的ip地址 打开你的电脑的命令行工具,ping你的github地址,忽略"/& ...