187. Repeated DNA Sequences

187. Repeated DNA Sequences

题目连接:https://leetcode.com/problems...code

这道题要求全部重复出现的序列,那么能够想到得用hash table,由于这里限制了是10个字符长的序列,因此每次实际上是去掉第一个letter,再加一个letter,这个思想和rabin karp挺像的,须要用int或者long来表示string。leetcode

public class Solution {
    public List<String> findRepeatedDnaSequences(String s) {
        Set<String> res = new HashSet();
        Set<Integer> dup = new HashSet();
        Map<Character, Integer> map = new HashMap();
        map.put('A', 0);  map.put('C', 1);  map.put('G', 2);  map.put('T', 3);
        int hash = 0;
        for(int i = 0; i < s.length(); i++) {
            hash = (hash << 2) | map.get(s.charAt(i));
            hash &= 0xfffff;
            if(i >= 9 && !dup.add(hash)) {
                res.add(s.substring(i-9, i+1));
            }
        }
        return new ArrayList(res);
    }
}
相关文章
相关标签/搜索