[Leetcode] Repeated DNA Sequences

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

 

大神的代码: http://blog.csdn.net/wzy_1988/article/details/44224749

-----------------------------------------------------------------

Native method:

 1 public class Solution {
 2     public List<String> findRepeatedDnaSequences(String s) {
 3         List<String> res = new ArrayList<>();
 4         if(s == null || s.length() < 2)
 5             return res;
 6         HashMap<String, Integer> hm = new HashMap<>();
 7         for(int i = 0; i < s.length() - 9; ++i) {
 8             String temp = s.substring(i, i + 10);
 9             if(hm.containsKey(temp))
10                 hm.put(temp, hm.get(temp) + 1);
11             else
12                 hm.put(temp, 1);
13         }
14         for(Map.Entry<String, Integer> entry: hm.entrySet()) {
15             if(entry.getValue() > 1)
16                 res.add(entry.getKey());
17         }
18         return res;
19     }
20 }

但是这种写法浪费了太多的空间。

---------------------------

Method 2: 利用二进制, 存储整数。

http://segmentfault.com/a/1190000002601702 

posted @ 2015-10-22 19:41  Phoebe815  阅读(149)  评论(0编辑  收藏  举报