【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 ...
随机推荐
- nodejs学习笔记_nodejs和PHP在基础架构上的差别--共享状态的并发
绝大多数对于Node.js的讨论都把关注点放在了处理高并发能力上,做开发的时候一定要明确node内部做出的权衡,以及node应用性能好的原因. node 为javascript引入了一个复杂的概念,: ...
- 《TCP/IP详细说明》读书笔记(17章)-TCP传输控制协定
1.TCP的服务 在一个TCP连接中.仅有双方进行彼此通信. TCP通过下列方式来提供可靠性: 1)应用数据被切割成TCP觉得最适合发送的数据块. 这和UDP全然不同,应用程序产生的数据报长度保持不变 ...
- Memory Architecture-SGA-Database Buffer Cache
启动instance:1.分配内存空间SGA 2.启动后台进程 内存结构:1.SGA 2.PGA 3.UGA 4.Software code areas SGA components:1.Databa ...
- JS中onload的各种使用
1.最简单的调用方式 直接写到html的body标签里面,如: <html> <body onload="func()"> </body> &l ...
- CSS3线性渐变linear-gradient
转自 http://www.w3cplus.com/content/css3-gradient CSS3的线性渐变 一.线性渐变在Mozilla下的应用 -moz-linear-gradient( [ ...
- iOS 滤镜 转载,原文见正文首行链接
转载自:http://blog.sina.com.cn/s/blog_5fb39f9101018gv7.html 直接上代码了: // // ViewController.m // 图片模糊处理 // ...
- URAL 1260 Nudnik Photographer DFS DP
题目:click here :这个题可以先dfs深搜下,规律dp dfs: #include <bits/stdc++.h> using namespace std; #define S ...
- OpenSSL命令---rsa
用途: Rsa命令用于处理RSA密钥.格式转换和打印信息.其实其用法和dsa的差不多. 用法: openssl rsa [-inform PEM|NET|DER] [-outform PEM|NET| ...
- CentOS 6.5(64bit)安装GCC4.8.2+Qt5.2.1(替换GCC的链接库)
截至目前,Qt的最新版本为5.2.1,CentOS的版本为6.5,GCC的版本为4.8.2,经过一番尝试,终于将Qt开发环境安装到了CentOS(64 bit)中,整个过程中有几个需要注意的地方,在这 ...
- Android 的平台碎片化问题
Android 的平台碎片化问题 看到篇不错的文章,转载过来. -------------------------------------- 与iOS开发相比,Android开发平添了不小的工作量,因 ...