Leetcode 187 Repeated DNA Sequences

Posted yfceshi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 187 Repeated DNA Sequences相关的知识,希望对你有一定的参考价值。


class Solution:
    # @param {string} s
    # @return {string[]}
    def findRepeatedDnaSequences(self, s):
        ans = []
        valCnt = dict()
        map = {‘A‘ : 0, ‘C‘ : 1, ‘G‘: 2, ‘T‘ : 3}
        sum = 0
        for x in range(len(s)):
            sum = (sum * 4 + map[s[x]]) & 0xFFFFF
            if x < 9:
                continue
            valCnt[sum] = valCnt.get(sum, 0) + 1
            if valCnt[sum] == 2:
                ans.append(s[x - 9 : x + 1])
        return ans


class Solution:
    # @param {string} s
    # @return {string[]}
    def findRepeatedDnaSequences(self, s):
        dict={}  
        for i in range(len(s)-9):  
            key = s[i:i+10]
            dict[key] = 1 if key not in dict else dict[key] + 1
        result = []
        for key in dict:
            if dict[key] > 1:   result.append(key)
        return result














以上是关于Leetcode 187 Repeated DNA Sequences的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode 187 Repeated DNA Sequences

LeetCode 187. Repeated DNA Sequences 20170706 第三十次作业

[LeetCode] 187. Repeated DNA Sequences 求重复的DNA序列

[LeetCode] 187. Repeated DNA Sequences

LeetCode 187. Repeated DNA Sequences(位运算,hash)

187. Repeated DNA Sequences