Repeated DNA Sequences

来源:互联网 发布:数据保护线有用吗 编辑:程序博客网 时间:2024/05/29 13:55

原题链接https://leetcode.com/problems/repeated-dna-sequences/

由于只有ATGC四个字母。我就简单的想到了位图对应的办法来做:

public class Solution {    public List<String> findRepeatedDnaSequences(String s) {      Set<String> result=new HashSet<String>();    boolean[] b=new boolean[1048575];    for (int i = 0; i < s.length()-9; i++) {    String tmp=s.substring(i, i+10);    int index=getIndex(tmp);    if(b[index]){    result.add(tmp);    }else{    b[index]=true;    }}return new ArrayList<String>(result);    }    public int getIndex(String s){    int index=0;    for (int i = 0; i < s.length(); i++) {switch (s.charAt(i)) {case 'A':index+=Math.pow(4, i)*0;break;case 'T':index+=Math.pow(4, i)*1;break;case 'G':index+=Math.pow(4, i)*2;break;case 'C':index+=Math.pow(4, i)*3;break;}}    return index;    }}


413ms通过,在java中处于平均值吧。

后来想办法优化,getindex方法被频繁调用,但是算法感觉不够精致,重新写了一下:

    private short[] table;    public int getIndex(String s){ <span style="white-space:pre"></span>int index=0;<span style="white-space:pre"></span>for (int j =0; j <s.length() ; j++) {<span style="white-space:pre"></span>index<<=2;<span style="white-space:pre"></span>index|=table[s.charAt(j)-'A'];<span style="white-space:pre"></span>}<span style="white-space:pre"></span>return index;    }

还有初始化在这里:
<span style="white-space:pre"></span>table=new short[20];    table[0]=0;    table[2] = 1;     table[6] = 2;    table[19] = 3;
321ms通过,已经在中上等了。


在网上找了找O(n)的算法,发现由于getIndex是顺序调用的,那么每次不必新计算,在上次结果的基础上左移再处理一下即可。代码如下:

private short[] table;    public List<String> findRepeatedDnaSequences(String s) {        if(s.length()<=10){return  new ArrayList<String>();}       table=new short[20];    table[0]=0;    table[2] = 1;     table[6] = 2;    table[19] = 3;    Set<String> result=new HashSet<String>();    boolean[] b=new boolean[1048575];    int index=0;        for (int j =0; j <10 ; j++) {index<<=2;    index|=table[s.charAt(j)-'A'];}    b[index]=true;    for (int i = 1; i < s.length()-9; i++) {    String tmp=s.substring(i, i+10);    //既然每次都有规律的移动    index<<=2;    index|=table[tmp.charAt(9)-'A'];    index&=0xFFFFF;    if(b[index]){    result.add(tmp);    }else{    b[index]=true;    }}return new ArrayList<String>(result);    }

312ms通过。实际上并没有快很多。

最终我也就提交这个了。没有找到再优化的办法,毕竟java相比其他语言慢了很多。


0 0
原创粉丝点击