【题目】

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 解题报告的更多相关文章

  1. 【LeetCode】187. Repeated DNA Sequences 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/repeated ...

  2. 【原创】leetCodeOj --- Repeated DNA Sequences 解题报告

    原题地址: https://oj.leetcode.com/problems/repeated-dna-sequences/ 题目内容: All DNA is composed of a series ...

  3. [LeetCode] 187. Repeated DNA Sequences 解题思路

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  4. [LeetCode] Repeated DNA Sequences 求重复的DNA序列

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  5. [Leetcode] Repeated DNA Sequences

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  6. LeetCode() Repeated DNA Sequences 看的非常的过瘾!

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  7. [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 ...

  8. LeetCode 187. 重复的DNA序列(Repeated DNA Sequences)

    187. 重复的DNA序列 187. Repeated DNA Sequences 题目描述 All DNA is composed of a series of nucleotides abbrev ...

  9. lc面试准备:Repeated DNA Sequences

    1 题目 All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &quo ...

随机推荐

  1. APUE学习之---------------进程

    离职了,交接期也有足够的时间了,可以在好好的再看一下APUE,想想上次详细的看还是在两年之前,虽然中间也偶尔会翻出来看看,但是由于工作上交集相对比较少一直没有去细读一下.现在正好是一段空挡期可以好好看 ...

  2. Qt webKit可以做什么(四)--实现本地QObject和JavaScript交互

    Qt webKit可以做什么(四)--实现本地QObject和JavaScript交互 Qt webKit可以做什么(四)--实现本地QObject和JavaScript交互

  3. 简单的REST的框架实现

    源代码下载地址:http://download.csdn.net/source/1662193 一. 认识REST REST软件架构是由Roy Thomas Fielding博士在2000年首次提出的 ...

  4. 【剑指offer】调整数组顺序

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/25829395 剑指offer上的第14题,九度OJ为了确保输出的结果的唯一性,在输出上做了 ...

  5. Cocos2d-x 3.0 使用TinyXml 解析XML文件

    在cocos2d-x 3.0中Xml解析已经不用自己找库了,已经为我们集成好了. text.xml <!--?xml version ="1.0" encoding =&qu ...

  6. JavaScript算法描述(一)

    function swap(arr,index1,index2){ var temp=arr[index1]; arr[index1]=arr[index2]; arr[index2]=temp; } ...

  7. 【转】Ubuntu 上编译Android出现cannot find -lstdc++解决办法

    [转]Ubuntu 上编译Android出现cannot find -lstdc++解决办法 在Ubuntu 12.04 x86_64机器上编译Android出现下面错误,是因为找不到32bit的li ...

  8. php随笔9-thinkphp OA系统 集成UEditor

    版本信息:thinkphp 3.1.3 full     UEditor 1.4.3.1 utf8-php 1.将EUditor放在项目public目录下. 2.在指定页面加载编辑器 <!-- ...

  9. 门面(Facade)模式--医院,保安系统实例

    门面(Facade)模式 http://www.cnblogs.com/zhenyulu/articles/55992.html

  10. PDO获取数据的方法fetch()、fetchAll()、setFetchMode()、bindColumn()

    PDO的数据获取方法与其他数据库扩展都非常类似,只要成功执行SELECT查询,都会有结果集对象产生.不管是使用PDO对象中的query()方法,还是使用prepare()和execute()等方法结合 ...