描写叙述:
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"].
思路:
1.非常显然,暴力求解也是一种方法。虽然该方法是不可能的。
2.我们首先来看字母 ”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中。以推断该子串是否出现过。
代码:
public ListfindRepeatedDnaSequences(String s) { List list=new ArrayList (); int strLen=s.length(); if(strLen<=10) return list; HashMap map=new HashMap (); int key=0; for(int i=0;i