Repeated DNA Sequences

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

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;
    }
}

 

posted @ 2015-05-22 05:49  世界到处都是小星星  阅读(138)  评论(0编辑  收藏  举报