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

Repeated DNA Sequences

时间:2015-05-22 07:03:52      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

比较难的一道题,但是还是能看懂

public class Solution {
    public List<String> findRepeatedDnaSequences(String s) {
        // ref http://blog.csdn.net/haiyi727/article/details/43752693
        int len = s.length();
        HashSet<Integer> tmp = new HashSet<Integer>();
        HashSet<Integer> resSet = new HashSet<Integer>();
        ArrayList<String> res = new ArrayList<String>();
        if(len<10) return res;
        HashMap<Character, Integer> map = new HashMap<Character, Integer>();
        map.put(‘A‘,0);
        map.put(‘T‘,1);
        map.put(‘C‘,2);
        map.put(‘G‘,3);
        int hash = 0;
        for(int i=0;i<len;i++){
            if(i<9){
                hash = (hash<<2) + map.get(s.charAt(i));
            }else{
                hash = (hash<<2)+ map.get(s.charAt(i));
                hash &= (1<<20)-1; // 取the last 20 digits
                if(tmp.contains(hash)&& !resSet.contains(hash)){
                    resSet.add(hash);
                    res.add(s.substring(i-9,i+1));
                }else{
                    tmp.add(hash);
                }
            }
        }
        return res;
    }
}

 

Repeated DNA Sequences

标签:

原文地址:http://www.cnblogs.com/jiajiaxingxing/p/4521306.html

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