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] Repeated DNA sequence

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

LeetCode(187)Repeated DNA Sequence

题目如下: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "AC...

LeetCode——Repeated DNA Sequences

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

LeetCode刷题-187. Repeated DNA Sequences

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

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

Repeated DNA Sequences   All DNA is composed of a series of nucleotides abbreviated as A, C, G...

算法作业HW15:LeetCode187 Repeated DNA Sequences

Description: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for ex...

LeetCode - Repeated DNA Sequences

https://leetcode.com/problems/repeated-dna-sequences/ All DNA is composed of a series of nu...

LeetCode Repeated DNA Sequences

LeetCode Repeated DNA Sequences All DNA is composed of a series of nucleotides abbreviated as A...

leetcode_Repeated DNA Sequences

我们首先来看字母 ”A" "C" “G" "T" 的ASCII码,分别是65, 67, 71, 84,二进制表示为 1000001, 1000011, 1000111, 1010100。可以看到它们的...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode Repeated DNA Sequence
举报原因:
原因补充:

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