【题目】

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. 犯罪团伙利用POS机刷信用卡积分转卖 年获利千万

      今年1月20日,广东省公安厅展示去年缴获的盗刷专用POS机. 今年1月20日,广东省公安厅展示了一批缴获的盗刷信用卡工具. 他们是一群靠信用卡谋生的年轻人,平均年龄不超过30岁. 他们将各银行信用 ...

  2. TCP编程的一个小例子

    TCP程序的服务器端与客户端的流程图 例子:服务器端等待客户端连接,若连接成功,则用户可以通过客户端向服务器端发送任意字符串,服务器端在收到字符串后,输出相关信息,在把接受到的字符串重新发生给客户端. ...

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

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

  4. Android API 中文(77)——AdapterView.OnItemSelectedListener

    前言 本章内容是android.widget.AdapterView.OnItemSelectedListener,版本为Android 2.3 r1,翻译来自"cnmahj",欢 ...

  5. 浏览器兼容——DOM事件封装函数

    //封装函数var eventUtil={    //添加事件    addHandler:function(element,type,handler){        if(element.addE ...

  6. JS中window.document对象

    小知识点注:外面双引号,里面的双引号改为单引号:                  在div里面行高设置和整个外面高度一样,才能用竖直居中,居中是行居中                  文本框取出来 ...

  7. Tomcat7.0.22在Windows下详细配置过程

    Tomcat7.0.22在Windows下详细配置过程 一.JDK1.7安装 1.下载jdk,下载地址:http://www.oracle.com/technetwork/java/javase/do ...

  8. vim插件管理之Vundle

    Vim是一个类似于Vi的著名的功能强大.高度可定制的文本编辑器,在Vi的基础上改进和增加了很多特性.正是由于其可定制的特性, 许许多多的Vim插件便诞生了.管理这些插件又成为我们最为头疼的问题,最近无 ...

  9. 一个i++和++i导致的严重的错误

    当我曾经在写一个strlen的实现时,用递归写出了如下的代码: int strlen(const char *s) { if(*s=='\0') ; else ; } 程序一运行就崩溃了,why!都是 ...

  10. perl学习(2) 基本数据类型等

    1.1.数字 所有数字格式内部一致,全部是double 7.25e45   == 7.25 * 1045 5.25 6.00 5.1-2.4          #5.1-2.4,2.7 10/3    ...