【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 ...
随机推荐
- Linux下安装VNC Server
操作系统centos6.5,在其之上安装vnc server,可利用windows上的vnc client远程登录. 1. 安装 yum install tigervnc-server.x86_64 ...
- python小游戏实现代码
早上逛CSDN首页就见到这么一篇教程.看了一下很有意思,就马上动手实现了一下.看看效果吧: 完整代码: # -*- coding: utf-8 -*- # 1 - Import library imp ...
- js中this的深入研究
this对象是函数在运行时由调用函数的对象决定的: 1.在全局对象中运行函数时,this等于window 2.当函数被作为某个对象的方法调用时, this等于那个对象. 需要注意的几点: 声明函数里的 ...
- C语言之猜数字游戏
猜数字游戏 猜数字游戏是以前功能机上的一款益智游戏,计算机会根据输入的位数随机分配一个符合要求的数据,计算机输出guess后便可以输入数字,注意数字间需要用空格或回车符加以区分,计算机会根据输入信息给 ...
- C陷阱与缺陷(三)
第三章 语义陷阱 3.1 指针与数组 C语言中只有一维数组,而且数组的大小必须字编译期就作为一个常数确定下来.数组中的元素可以是另外一个数组.任何一个数组下标运算都等同于一个对应的指针运算.int a ...
- EAN-13 条码(又称GTIN-13 条码)
EAN全名为European Article Number(欧洲商品条码),在1977年时由欧洲几个主要工业国家共同发展出来的,后来变成国际商品条码系统.台湾在1985年加入EAN会员,现在我们买东西 ...
- session 共享
目前大多数大型网站的服务器都采用了分布式的部署方式,但是session是在服务器端保存的,如果用户跳转到其他服务器的话,session就会丢失,于是就有了分布式系统的session共享问题. sess ...
- poj 1442 Black Box(优先队列&Treap)
题目链接:http://poj.org/problem?id=1442 思路分析: <1>维护一个最小堆与最大堆,最大堆中存储最小的K个数,其余存储在最小堆中; <2>使用Tr ...
- linux内核源码阅读之facebook硬盘加速flashcache之三
上一节讲到在刷缓存的时候会调用new_kcahed_job创建kcached_job,由此我们也可以看到cache数据块与磁盘数据的对应关系.上一篇:http://blog.csdn.net/lium ...
- Oracle 11g RAC 环境下单实例非缺省监听及端口配置
如果在Oracle 11g RAC环境下使用dbca创建单实例数据库后,Oracle会自动将其注册到缺省的1521端口及监听器.大多数情况下我们使用的为非缺省监听器以及非缺省的监听端口.而且在Orac ...