首页 > 其他 > 详细

Repeated DNA Sequences

时间:2015-05-22 07:02:38      阅读:219      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!