【LeetCode】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"].
【解析】
题意:找出DNA序列中全部出现次数大于1的长度为10的子串。
我认为题目给的样例有问题,样例中除了给出的两个子串,还有“ACCCCCAAAA", "AACCCCCAAA", "AAACCCCCAA", "AAAACCCCCA"也都符合条件。
注意题目中第一段连续的C是5个,第二段连续的C是6个。(Update on 2015-07-22)
參考 https://oj.leetcode.com/discuss/24478/i-did-it-in-10-lines-of-c 我来解释一下思路:
首先看这道题的Tags是Hash Table和Bit Manipulation。那么怎样把字符串转化为位操作呢?我们首先来看字母 ”A" "C" “G" "T" 的ASCII码。各自是65, 67, 71, 84。二进制表示为 1000001, 1000011, 1000111, 1010100。能够看到它们的后四位是不同。所以用后四位就能够区分这四个字母。一个字母用3bit来区分,那么10个字母用30bit就够了。
用int的第29~0位分表表示这0~9个字符。然后把30bit转化为int作为这个子串的key,放入到HashTable中,以推断该子串是否出现过。
【Java代码】
public class Solution {
public List<String> findRepeatedDnaSequences(String s) {
List<String> ans = new ArrayList<String>();
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int key = 0;
for (int i = 0; i < s.length(); i++) {
key = ((key << 3) | (s.charAt(i) & 0x7)) & 0x3fffffff;
if (i < 9) continue;
if (map.get(key) == null) {
map.put(key, 1);
} else if (map.get(key) == 1) {
ans.add(s.substring(i - 9, i + 1));
map.put(key, 2);
}
}
return ans;
}
}
【补充】
假设说不记得A。C,G,T的ASCII码了。并且也不想计算它们的二进制表示,那么能够用一种替代的方法。即把A,C,G,T映射成0。1。2。3,这样用两个bit就能够区分它们00,01。10,11了。还有这里是长度为10的子串。并且这四个字符用3bit就能够区分,加起来总共30bit,假设子串长度再长些,或者字符种类再多些须要3bit以上来区分,总共bit数超过32,那么採用映射不失为一种非常好的解决方法。可參见http://bookshadow.com/weblog/2015/02/06/leetcode-repeated-dna-sequences/
【LeetCode】Repeated DNA Sequences 解题报告的更多相关文章
- 【LeetCode】187. Repeated DNA Sequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/repeated ...
- 【原创】leetCodeOj --- Repeated DNA Sequences 解题报告
原题地址: https://oj.leetcode.com/problems/repeated-dna-sequences/ 题目内容: All DNA is composed of a series ...
- [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] Repeated DNA Sequences 求重复的DNA序列
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- [Leetcode] Repeated DNA Sequences
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- LeetCode() Repeated DNA Sequences 看的非常的过瘾!
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- [LeetCode] Repeated DNA Sequences hash map
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- 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 ...
随机推荐
- BZOJ 1088 扫雷Mine (递推)
题解:如果确定了第一排前两个数,那么剩下的数是唯一确定的,所以只要分情况讨论即可. #include <cstdio> #include <cstring> int n,a[1 ...
- Oracle DBA常用的系统表
1.2 DBA常用的表1.2.1 dba_开头 dba_users数据库用户信息 dba_segments 表段信息 dba_extents 数据区信息 dba_ob ...
- 笔试题:金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)->(一千零一拾一元整)输出
收集这道题目原因是以前做过,但是实现的很麻烦,这次看到别人写的感觉简单易懂. 从一个pdf看到,出处就不贴了 = .= public class RenMingBi { private static ...
- How to setup linked servers for SQL Server and Oracle 64 bit client
感谢作者:Tim Ford. 图文并茂. 原帖地址: http://www.mssqltips.com/sqlservertip/1433/how-to-setup-linked-servers-fo ...
- UVA 10312 - Expression Bracketing(数论+Catalan数)
题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=1253">10312 - Exp ...
- Ext JS学习第十五天 Ext基础之 Ext.DomQuery
此文同来记录学习笔记 •Ext.dom.Query 嗯,这个类一看就是到时做什么事儿的,不用我去过多的解释了.这个类一共提供了8个方法供开发人员去使用. •要说最常用的方法,无非就是Ext.query ...
- JSON Editor 中文文档
JSON Editor JSON Editor 根据定义的JSON Schema 生成了一个Html 表单来对JSON进行编辑.它完整支持JSON Schema 的版本3和版本4,并且它集成了一些流行 ...
- MAVEN入门(二)
一.IDEA+MAVEN+Tomcat7 创建一个简单的Web app 1.用IDEA创建一个maven项目 注意: 红色部分一定要自己手选本地配置好的maven_home的地址,否则IDEA会选用内 ...
- Hadoop运行中遇到的错误调试
在运行Hadoop的过程中遇到的最多的问题就是DataNode不能正常的启动,各种问题都有可能,说一下我遇到的两种情况: (1)第一种情况是Master的防火墙没有关闭.这样在启动Hadoop的时候, ...
- java代码获取ip地址
public class IpTool { public static void main(String[] args) { IpTool ipTool=new IpTool(); System.ou ...