码迷,mamicode.com
首页 > 其他好文 > 详细

[Leetcode] Repeated DNA Sequences

时间:2015-10-22 20:56:17      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

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: 利用二进制, 存储整数。

 

[Leetcode] Repeated DNA Sequences

标签:

原文地址:http://www.cnblogs.com/Phoebe815/p/4902572.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!