leetcode Repeated DNA Sequence

原创 2016年08月29日 19:24:45

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"].

Show Company Tags
Show Tags

Have you met this question in a real interview? 

Yes
 

这道题思路是这样的:ACGT的ASCII码值后三位是不同的,分别为001 011 111 100 也就是说通过3bit可以表示一个基因,30bit可以表示一个10-letter-long的基因组,我们只需要将这个30bit的整数存入hashmap,然后每次判断新的整数是否存在于map中即可:

public List<String> findRepeatedDnaSequences(String s) {
    List<String> list=new ArrayList<>();
    Map<Integer,Integer> map=new HashMap<>();
    int gene=0;
    for(int i=0;i<s.length();i++){
        gene=(gene<<3|(s.charAt(i)&0x7))&(0x3fffffff);
        if(i<9) continue;
        if(map.get(gene)==null){
            map.put(gene,1);
        }
        else if(map.get(gene)==1){
            list.add(s.substring(i-9,i+1));
            map.put(gene,2);
        }

    }
    return list;
}

leetcode_c++:哈希: Repeated DNA Sequences(187)

All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: “ACGAATTC...
  • mijian1207mijian
  • mijian1207mijian
  • 2016年07月09日 14:45
  • 195

【LeetCode】Repeated DNA Sequences 解题报告

【题目】 All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "A...
  • ljiabin
  • ljiabin
  • 2015年03月20日 11:19
  • 5607

[leetcode-187]Repeated DNA Sequences(java)

问题描述: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: “A...
  • zdavb
  • zdavb
  • 2015年08月28日 22:20
  • 630

[LeetCode]Repeated DNA Sequences,解题报告

目录目录 前言 题目 Native思路 二进制思路 AC前言最近在LeetCode上能一次AC的概率越来越低了,我这里也是把每次不能一次AC的题目记录下来,把解题思路分享给大家。题目All DNA i...
  • zinss26914
  • zinss26914
  • 2015年03月14日 12:09
  • 3455

leetcode 187: Repeated DNA Sequences

Total Accepted: 1161 Total Submissions: 6887 All DNA is composed of a series of nucleotides abb...
  • xudli
  • xudli
  • 2015年02月09日 07:29
  • 4651

187. Repeated DNA Sequences Leetcode Python

All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTC...
  • hyperbolechi
  • hyperbolechi
  • 2015年03月16日 11:21
  • 1095

LeetCode187——Repeated DNA Sequences

All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTC...
  • booirror
  • booirror
  • 2015年08月18日 23:47
  • 1020

[LeetCode]Repeated DNA Sequence

原题链接 参考友链 public class Solution { public List findRepeatedDnaSequences(String s) { ...
  • Three_Thousand_World
  • Three_Thousand_World
  • 2015年08月12日 09:38
  • 147

Leetcode: Repeated DNA Sequence

All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTC...
  • denisewu
  • denisewu
  • 2015年03月15日 15:01
  • 225

[leetcode] Repeated DNA sequence

All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTC...
  • uknowfigo
  • uknowfigo
  • 2015年03月05日 14:39
  • 148
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode Repeated DNA Sequence
举报原因:
原因补充:

(最多只允许输入30个字)