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 ...
随机推荐
- leetcode295
public class MedianFinder { List<int> list = null; ; /** initialize your data structure here. ...
- Appium -选择、操作元素4
webvie的测试 混合(Hybrid)应用 一部分是原生界面和代码,而一部分是内嵌网页 比如微信.支付宝 内嵌了一个浏览器内核,由浏览器内核实现的 安卓应用中的内嵌的展示网页内容的模块,我们称之为w ...
- C++11并发之std::thread<转>
最近技术上没什么大的收获,也是悲催的路过~ 搞一点新东西压压惊吧! C++11并发之std::thread 知识链接: C++11 并发之std::mutex C++11 并发之std::atomic ...
- 排序NB三人组
排序NB三人组 快速排序,堆排序,归并排序 1.快速排序 方法其实很简单:分别从初始序列“6 1 2 7 9 3 4 5 10 8”两端开始“探测”.先从右往左找一个小于6的数,再从左往 ...
- select as table
select order_time, max(sum_price) from (SELECT order, sum(price) as sum_price FROM orders group by o ...
- 更改html代码后网页不更新
写了一个非常简单的 html 页面,只有简单的跳转功能,但是在 Eclipse 下更改代码后用 chrome 浏览器打开时还是显示原来的网页.开始我以为是网页有错误或者有不规范的地方,因为我编写的是 ...
- MemSQL 架构初探(转)
MemSQL 自称是最快的内存数据库.目前已发布了2.5版本. MemSQL 具有以下特点 1 高效的并行,尤其是分布式的MemSQL. 2 高效的并发,采用lock-free的内存数据结构skip ...
- 尚硅谷springboot学习15-日志框架1-入门
引子 小张:开发一个大型系统 1.System.out.println(""):将关键数据打印在控制台:去掉?写在一个文件? 2.框架来记录系统的一些运行时信息:日志框架 : ...
- Easy-to-Learn English Travel Phrases and Vocabulary!
Easy-to-Learn English Travel Phrases and Vocabulary! Share Tweet Share Tagged With: Real Life Englis ...
- springmvc简单教程
IDEA建立Spring MVC Hello World 详细入门教程(转自) 引子,其实从.NET转Java已经有几个月时间了,项目也做了不少,但是很多配置都是根据公司模板或者网上教程比忽略画瓢 ...