码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 187 Repeated DNA Sequences

时间:2015-07-04 15:33:25      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:


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

标签:

原文地址:http://blog.csdn.net/andrew9tech/article/details/46755199

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!