其他分享
首页 > 其他分享> > leetcode-187-重复的DNA序列

leetcode-187-重复的DNA序列

作者:互联网

题目描述:

方法一:

class Solution:
    def findRepeatedDnaSequences(self, s: str) -> List[str]:
        n = 10
        d = {}
        for i in range(len(s)-9):
            if s[i:i+10] in d:
                d[s[i:i+10]] = True
            else:
                d[s[i:i+10]] = False
        return [i for i in d if d[i]]

方法二:

class Solution:
    def findRepeatedDnaSequences(self, s: str) -> List[str]:
        n = 10
        d = collections.defaultdict(int)
        for i in range(len(s)-9):
            d[s[i:i+10]] += 1
        return [i for i in d if d[i]>1]

方法三:

class Solution:
    def findRepeatedDnaSequences(self, s: str) -> List[str]:
        n=len(s)-9 
        d=set() 
        ans=set() 
        for i in range(n): 
            if s[i:i+10] not in d: 
                d.add(s[i:i+10]) 
            elif s[i:i+10] not in ans: 
                ans.add(s[i:i+10]) 
        return list(ans)

 

标签:10,DNA,self,List,187,str,ans,return,leetcode
来源: https://www.cnblogs.com/oldby/p/11217533.html