美文网首页
[LeetCode By Python] 187. Repeat

[LeetCode By Python] 187. Repeat

作者: 乐乐可爱睡觉 | 来源:发表于2016-06-14 16:10 被阅读251次

一、题目

Repeated DNA Sequences

二、解题

题意是找出给出字符串里面,连续10个字母出现多次的串。

首先想到的是一重循环,然后用字典储存每个串出现的次数,最后找出值大于1的key。

三、尝试与结果

class Solution(object):
    def findRepeatedDnaSequences(self, s):
        resultDict = dict()
        if len(s) <= 10:
            return []
        for i in range(0,len(s)-9):
            key = s[i:i+10]
            if not resultDict.has_key(key):
                resultDict[key] = 1
            else:
                resultDict[key] = resultDict[key] + 1

        resultlist = list()
        for key in resultDict:
            if resultDict[key] > 1:
                resultlist.append(key)

        return resultlist

结果:AC

四、学习与记录

这里会有可能报空间溢出的限制,只用把ACTG换成二进制就可以了

相关文章

网友评论

      本文标题:[LeetCode By Python] 187. Repeat

      本文链接:https://www.haomeiwen.com/subject/tfsudttx.html