[位運算]187. Repeated DNA Sequences

題目:187. Repeated DNA Sequences[Medium]
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序列中所有長度大于10且出現次數大于1的子串。

方法一:用HashMap存儲所有子串,結果:Time Limit Exceeded

class Solution {
    public List<String> findRepeatedDnaSequences(String s) {
        List<String> res = new ArrayList<String>();
        if(s.length() <= 10) return res;
        
        Map<String,Integer> strmap = new HashMap<String, Integer>();
        int i =0;
        
        while( i <= s.length()-10){
            String temp = s.substring(i, i+10);
            if(!strmap.containsKey(temp)){
                strmap.put(temp,1);
                i++;
            }else{
                if(strmap.get(temp) == 1){
                    res.add(temp);
                    strmap.put(temp,-1) ; //had be add to res
                    i++;
                }
            }
        }
        return res;
    }
}

方法二:位運算
Runtime: 63 ms
對于A,C,G,T四個字符其二進制表示為如下,僅有最后三位不同。

A: 0100 0001
C: 0100 0011  
G: 0100 0111  
T: 0101 0100

每一位用1bit表示,10個字符供需要10x3 = 30bit。一個int有32bit,可以表示一個字符串。

注意

0x7ffffff 是 111...1111 , 一共3+6*4 = 27bit位個1
substring 的范圍是前閉后開:[0, 10) 取得是->[0,9]

class Solution {
    public List<String> findRepeatedDnaSequences(String s) {
        List<String> res = new ArrayList<String>();
        if(s.length() <= 10) return res;
        
        Map<Integer,Integer> strmap = new HashMap<Integer, Integer>();//substring, 出現次數
        int i =0;
        int mask = 0x7ffffff; //111...1111  一共3+6*4 = 27bit位個1
        int cur =0;
        while( i< 9 ) {
            cur = cur<<3 | (s.charAt(i) & 7); i++;
        }
        //i =9
        while( i < s.length()){
            cur = ((cur & mask) << 3) | ((int)s.charAt(i) & 7); 
            //((cur & mask) << 3) |:取cur的后27位再補3個0,再加上i的三位
            if(!strmap.containsKey(cur)){
                strmap.put(cur,1);
            }else{
                if(strmap.get(cur) == 1){
                    res.add(s.substring(i-9,i+1)); //[i-9, i+1)
                    strmap.put(cur,-1) ; //had be add to res
                }
            }
             i++;
        }
        return res;
    }
}

方法三:
Runtime: 41 ms
在solution里看到的,更快更節約時間。

在set.add(obj)方法里,如果obj已在set中存在,該方法會返回false。

class Solution {
    public List<String> findRepeatedDnaSequences(String s) {
        Set set = new HashSet();
        Set repeat = new HashSet();
        
        for(int i=0; i<s.length()-9;i++){
            
            if(!set.add(s.substring(i,i+10))){
                repeat.add(s.substring(i,i+10));
            }
        }
        return new ArrayList(repeat);
        
    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 與文者字清合作,我上片,他下片 作者/若雪 煙雨打濕了江南 小橋流水彎彎 枝頭雙鵲呢喃 清風浮動楊柳岸 芷若悠悠 ...
    玉人初上閱讀 368評論 15 14
  • 記 一場 跑路時長 長達 相聚時長 提傘 也要約的 會 。 可能怕以后 時間 距離 真的淡了感情 可能 以前經歷過...
    _覓小金桔閱讀 171評論 1 1
  • 我是一個胖子,一米六五的個兒,體重卻超過80公斤。雖然我常在朋友面前自詡心寬體胖,表面上表現得滿不在乎,但我心里苦...
    劉芷源07閱讀 482評論 9 8
  • 我上學的時候理想的對象是會說俏皮話挑逗人的,有點文藝氣質會寫詩的,會把白襯衫穿成一道風景線的,干凈清新、側臉英俊的...
    茜喵閱讀 1,242評論 3 17