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 ...
随机推荐
- Linux服务器mysql,nginx等自动停止的排查,以及解决方法
最近发现自己使用的某台云服务武器只要稍微流量大一点,就会出现莫名的的数据库连接错误的情况 ,然后连上服务器查看,发现mysql进程被结束了,经常出现这种情况,这还怎么得了,然后第一时间查看mysql日 ...
- 机器学习进阶-光流估计 1.cv2.goodFeaturesToTrack(找出光流估计所需要的角点) 2.cv2.calcOpticalFlowPyrLK(获得光流检测后的角点位置) 3.cv2.add(进行像素点的加和)
1.cv2.goodFeaturesToTrack(old_gray, mask=None, **feature_params) 用于获得光流估计所需要的角点参数说明:old_gray表示输入图片, ...
- JConsole 配置
Tomcat 1:修改catalina.sh文件如下 JAVA_OPTS="-Djava.rmi.server.hostname=XXX.XXX.XXX.XXX -Dcom.sun.mana ...
- Dubbo的优化 --- 开发时使用
开发时的三个优化: 1.开发者在本地开发的时候启动Dubbo比较麻烦,所以采用直接连接的配置: 2.开发者本地开发时会打断点调试,会超过Dubbo默认的超时时间1s,所以需要全局设置超时时间: 3.开 ...
- Linux主机如何用ssh去登录docker容器的步骤
进入终端,sudo -i,切换root,输入docker -d 打开另一个终端,切换root,输入docker search ubuntu,大概如下结果: NAME ...
- Cannot detect Web Project version. Please specify version of Web Project through Maven project property <webVersion>. E.g.: <properties> <webVersion>3.0</webVersion> </properties>
鼠标放在问题出会出现set web project version to 3.0和set web project version to 3.1两个选项 随便选一个版本就好了
- mysql decimal
可能做程序的人都知道,float类型是可以存浮点数(即小数类型),但是float有个坏处,当你给定的数据是整数的时候,那么它就以整数给你处理. 这样我们在存取货币值的时候自然遇到问题,我的defaul ...
- 什么是socket?
1.七层协议简化为四层:应用层.传输层.网络层.链路层:2.套接字是应用层和TCP/IP协议族通信间的软件抽象层,将TCP/IP层复杂的操作抽象为几个简单的接口供应用层调用实现进程在网络中的通信:本地 ...
- ajaxfileupload.js上传文件兼容IE7及以上版本
要兼容IE789,要修改ajaxfileupload.js;要将此处的代码替换掉 if(window.ActiveXObject) { var io = document.createElement( ...
- SQLServer 的数据分页:
假设现在有这样的一张表:CREATE TABLE test( id int primary key not null identity, names varchar(20))然后向里面插入大约1000 ...