187. Repeated DNA Sequences (String; Bit)
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"].
思路I:遍历string,每次截取10个字符,判断出现次数。
Result: Time Limit Exceeded
思路II:字符数较少=>用数字表示字符=>用bitmap来表示字符串,好处:节省空间
比如本题只可能出现4种字符=>可表示为0,1,2,3,即可以用2bits来表示=>字符原本一个字符占1 byte = 8 bits,现在只要2 bits
class Solution {
public:
int getVal(char ch) {
if (ch == 'A') return ;
if (ch == 'C') return ;
if (ch == 'G') return ;
if (ch == 'T') return ;
} vector<string> findRepeatedDnaSequences(string s) {
int sLen = s.length();
unsigned int val=;
char mp[*]={};
vector<string> ret;
string str; if(sLen < ) return ret; for(int i = ; i < ; i++){
val <<=;
val |= getVal(s[i]);
} for(int i = ; i < sLen; i++){
val <<= ;
val |= getVal(s[i]);
val &= 0xFFFFF;
if(++mp[val] == ){
str = s.substr(i-,);
ret.push_back(str);
}
} return ret;
}
};
187. Repeated DNA Sequences (String; Bit)的更多相关文章
- [LeetCode] 187. Repeated DNA Sequences 求重复的DNA序列
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- leetcode 187. Repeated DNA Sequences 求重复的DNA串 ---------- java
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- Java for LeetCode 187 Repeated DNA Sequences
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- 187. Repeated DNA Sequences
题目: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: " ...
- [LeetCode#187]Repeated DNA Sequences
Problem: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: ...
- [LeetCode] 187. Repeated DNA Sequences 解题思路
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- 【LeetCode】187. Repeated DNA Sequences
题目: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: " ...
- 187. Repeated DNA Sequences重复的DNA子串序列
[抄题]: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &qu ...
- *187. Repeated DNA Sequences (hashmap, one for loop)(difference between subsequence & substring)
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
随机推荐
- 白鹭引擎 - 对象的添加与删除 ( 开关效果 addChild, removeChild )
class Main extends egret.DisplayObjectContainer { /** * Main 类构造器, 初始化的时候自动执行, ( 子类的构造函数必须调用父类的构造函数 ...
- Linux定时任务 结合PHP实现实时监控
首先说说cron,它是一个linux下的定时执行工具.根用户以外的用户可以使用 crontab 工具来配置 cron 任务. 所有用户定义的 crontab 都被保存在/var/spool/cron ...
- [bcc32 Error] ws2def.h(231): E2238 Multiple declaration for 'sockaddr'
[bcc32 Error] ws2def.h(231): E2238 Multiple declaration for 'sockaddr' Full parser context ksrGe ...
- centos7 自动定时备份mysql数据库
shell脚本:mysqlbak.sh #!/bin/bashPATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbinexpo ...
- 正则前面的 (?i) (?s) (?m) (?is) (?im)
(?i) 表示所在位置右侧的表达式开启忽略大小写模式(?s) 表示所在位置右侧的表达式开启单行模式(?m) 表示所在位置右侧的表示式开启指定多行模式(?is) 更改句点字符 (.) 的含义,以使它与每 ...
- php初级之数组与 类初级
PHP 是后端脚本语言,回顾一下: 1.1 PHP 数组: <?php $user = array(); // 定义一个数组 $user[0] = 'zhangsan'; // 给数组赋值 $u ...
- Weed-FS 接口 master、volume 服务接口(转)
目录结构 weed-fs master 服务接口,分配文件 id,查找 volume,volume 服务接口,在指定的 volume 服务创建指定的 volume,检查 volume 服务的状态. ...
- Dictionary,hashtable, stl:map有什么异同?
相同点:字典和map都是泛型,而hashtable不是泛型. 不同点:三者算法都不相同 Hashtable,看名字能想到,它是采用传统的哈希算法:探测散列算法,而字典则采用的是散列拉链算法,效率较高, ...
- 使用git pull与本地文件冲突
出错信息如下: error: Your local changes to 'c/environ.c' would be overwritten by merge. Aborting. Please, ...
- Haskell语言学习笔记(85)Async
安装 async $ cabal install async async-2.2.1 installed async / wait / concurrently async :: IO a -> ...