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

[LeetCode]Repeated DNA Sequences

时间:2015-03-13 12:48:59      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:java   leetcode   

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"].
使用位存储,每个char型占两个字节,这样可以节省很多空间


public class Solution {
    public List<String> findRepeatedDnaSequences(String s) {
    	List<String> list = new ArrayList<>();
    	Map<Integer,Integer> map = new HashMap<>();
    	for(int i=0;i<s.length()-9;i++){
    		int bit =0;
    		for(int j=i;j<i+10;j++){
    			switch(s.charAt(j)){
    				case 'A': 
    					bit += 0;
    					break;
    				case 'C':
    					bit += 1;
    					break;
    				case 'G':
    					bit += 2;
    					break;
    				case 'T':
    					bit += 3;
    					break;
    			}
    			bit<<=2;
    		}
    		Integer value = map.get(bit);
    		if(value==null){
    			map.put(bit, 1);
    		}else if(value == 1){
    			list.add(s.substring(i,i+10));
    			map.put(bit, 2);
    		}
    	}
    	return list;
    }
}


[LeetCode]Repeated DNA Sequences

标签:java   leetcode   

原文地址:http://blog.csdn.net/guorudi/article/details/44238379

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