LeetCode DNA重复序列

33 篇文章 0 订阅

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"].

所有的DNA是由核苷酸序列组成,分别用A,C,G 和T来表示,例如,"ACGAATTCCG"。 我们学习DNA时,在DNA中识别重复序列有时很重要。

要求,写一个函数找到出现过多次,并且所包含10个英文字母的子序列。

//

分析,最直接的方法是,创建一个HashTable来记录所有的长度为10的子序列。

组后输出出现次数大于1的。

然而,没办法通过,leetcode, 报错原因是超过内存限制。

public List<String> findRepeatedDnaSequences(String s) {
        List<String> list = new ArrayList<String>();
        if(s == null || s.length() < 10) return list;
        
        HashMap<String, Integer> table = new HashMap();
        int L = 10;
        for(int i = 0; i <= s.length() - L; i++){
            String sub = s.substring(i, i + L);
            if(table.containsKey(sub)){
                table.put(sub, table.get(sub) + 1);
            }else table.put(sub, 1);
        }
        
        Iterator<String> it = table.keySet().iterator();
        while(it.hasNext()){
            String key = it.next();
            if(table.get(key) > 1)
                list.add(key);
        }
        return list;
    }

改进方案:

1. 第一输出List改用LinkedList而非ArrayList(原因在于两个List性质不同)。

2. HashMap中的Key是长度为string,也就是说,他所占有的内存大小约为2 * 10字节。继续分析发现所有字符串只包含四个不同的字符(A,C,G 和T),我们可以用2 bit来表示。这样自然会想到把string映射到一个整数空间,因为每个字符串可以唯一的用2*10 bit来表示。

3. HashMap中的值Value,也可以换成Boolean(只有1bit)。

一下是修改后的算法。


private int myHash(String s){
        int n = 0;
        for(int i = 0; i < s.length(); i++){
                n <<=2;
                char c = s.charAt(i);
                if(c == 'C'){
                    n += 1;
                }else if(c == 'G'){
                    n += 2;
                }else if(c == 'T'){
                    n += 3;
                }
        }
        return n;
    }
    
    public List<String> findRepeatedDnaSequences(String s) {
        List<String> list = new LinkedList<String>();
        if(s == null || s.length() < 10) return list;

        HashMap<Integer, Boolean> table = new HashMap<Integer, Boolean>();
        int L = 10;
        for(int i = 0; i <= s.length() - L; i++){
            String sub = s.substring(i, i + L);
            int hs = myHash(sub);
            if(table.containsKey(hs)){
                if(!table.get(hs)) list.add(sub);
                table.put(hs, true);
            }else{
                table.put(hs, false);
            }
        }
        return list;
    }




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值