【Leetcode】【Medium】Repeated DNA Sequences
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"].
思考:
1、这是一道非常典型的求重复字符子串的问题,涉及到记录和查找,使用hash表最省时间,hash表需要key值取数字,自然考虑到将字符串转为ASCII码;
2、由于字母只有4种可能,那么直观的想到将A、C、G、T,分别对应:00,01,10,11,因此一个10个字母组成的子串,就可以表示为20个比特的int;
解题:
1、对原字符串进行遍历,每读一个字符,将其转为2位的bit值,并记录在int的对应位上;
2、读到10位之后,加入hash表中,并继续读取;当hash表中已经有对应数字存在,则为重复的字符串,记录在返回数组中;注意避免记录多次重复的字符串;
代码优化:
1、由于hash表只需记录当前值是否只出现一次,所以使用<int, bool>的键值对类型就可以了,bool变量true则只出现一次,出现其他次false;
2、虽然开始将ACGT转为00/01/10/11,但是发现重复后,不需要再转回来,因为字符串是顺序遍历的,遍历到重复后,包括当前遍历字符在内的前10个字符就是重复的子字符串;
3、每次ACGT转化,也会产生一定开销,由于ACGT的ASCII码值后三位分别为:001/011/111/100互不相同,而使用3个bit表示一个字母,10个字母30bit不会超过int的范围;因此使用三个bit来表示字母较好;
代码:
class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
unordered_map<int, bool> hmap;
vector<string> ret;
int dna_bit = ;
int bits_cut = 0x3FFFFFFF; for (int i = ; i < s.length() && i < ; ++i) {
dna_bit = (dna_bit << ) | (s[i] & );
} for (int i = ; i < s.length(); ++i) {
dna_bit = (dna_bit << ) | (s[i] & );
dna_bit = dna_bit & bits_cut;
if (hmap.find(dna_bit) != hmap.end()) {
if (hmap[dna_bit]) {
ret.push_back(s.substr(i - , ));
hmap[dna_bit] = false;
}
} else {
hmap[dna_bit] = true;
}
} return ret;
}
};
另:
还可以将第一个循环去除,因为ACGT字母后三位都不全为0,所以如果字母不到10个在hash表中肯定是唯一的,如果hash表插入的开销很小,可以省略第一个循环,本文中没有省略;
附录:
C++ hash表高效使用
【Leetcode】【Medium】Repeated DNA Sequences的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 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: &quo ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【LeetCode】187. Repeated DNA Sequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/repeated ...
- 【leetcode】Repeated DNA Sequences(middle)★
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
随机推荐
- Python 自定义iterator生成器
#计数版 class countdown(object): def __init__(self,start): self.start = start def __iter__(self): retur ...
- Js框架设计之DomReady
一.在介绍DomReady之前,先了解下相关的知识 1.HTML是一种标记语言,告诉我们这页面里面有什么内容,但是行为交互则要通过DOM操作来实现,但是注意:不要把尖括号里面的内容看作是DOM! 2. ...
- MySQL插入记录 insert
一.insert insert tb_name [(col_name,......)] {values | value} ({expr | default } ....... ) , ( ... ) ...
- 使用BeanUtils封装数据时数据类型的转换
//获得表单数据 Map<String, String[]> properties = request.getParameterMap(); User user = new User(); ...
- SQL Cookbook—数字、日期
1.计算不包含最大值和最小值的均值2.把字母数字串转换为数值3.更改累计和中的值–显示存款或取款后的值4.加减日.月.年5.计算两个日期之间的天数6.确定两个日期之间的工作日数目表EMP中,计算BLA ...
- 2014年北京网络赛 Instrusive HDU 5040 题解 优先队列
网赛的时候看了这道题,发现就是平常的那种基础搜索题. 由于加了一个特殊条件:可以一次消耗3秒或原地停留1秒. 那就不能使用简单的队列了,需要使用优先队列才行. 题意 告诉一副地图:一个起点,一个终点, ...
- Spring Boot学习笔记-配置devtools实现热部署
写在前面 Spring为开发者提供了一个名为spring-boot-devtools的模块来使Spring Boot应用支持热部署,提高开发者的开发效率,无需手动重启Spring Boot应用. de ...
- Oracle 12c心得
1.重新启动Listener后,远程客户端登录不了,只能全新启动Oralce服务才能正常,经分析,用Net Manager增加一个服务器的IP地址的监听. 执行 net start 监听服务名 再远程 ...
- h5移动端设置键盘搜索
点击键盘上的搜索按钮实现页面跳转 <form action="#list?goods_title={{message?message:''}}" @submit.preven ...
- WebAPI搭建(二) 让WebAPI 返回JSON格式的数据
在RestFul风格盛行的年代,对接接口大多数人会选择使用JSON,XML和JSON的对比传送(http://blog.csdn.net/liaomin416100569/article/detail ...