博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode187. Repeated DNA Sequences
阅读量:4222 次
发布时间:2019-05-26

本文共 1197 字,大约阅读时间需要 3 分钟。

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.

第一种方法,像切猪肉那样,将s分成两段,然后比较,可惜TLE了

class Solution(object):    def findRepeatedDnaSequences(self,s):        list_=list(s)        ans=[]        while  len(list_)>10:            sub_string="".join([list_[i] for i in range(10)])            sub_string2=s[s.find(sub_string)+len(sub_string):]            if sub_string in sub_string2 and sub_string not in ans:                ans.append(sub_string)            list_.remove(list_[0])        return ans

第二种方法,存在字典中,如果在字典中出现且在ans中未曾录入的,则ans.append(),注意细节,range(len(s)-9)而不是-10

class Solution(object):    def findRepeatedDnaSequences(self,s):        doc={}        ans=[]        if len(s)<=10:            return ans        for i in range(len(s)-9):            temp=s[i:i+10]            if temp not in doc:                doc.setdefault(temp,None)            else:                if temp not in ans:                    ans.append(temp)        return ans

转载地址:http://lwqmi.baihongyu.com/

你可能感兴趣的文章
Linux命令行与shell编程第3章基本的shell
查看>>
Linux命令行与shell编程第4章 更多的bash shell命令
查看>>
4 51 单片机最小系统
查看>>
6 51点亮第一个LED
查看>>
8 51 LED流水灯
查看>>
Multisim 14.0 搭建并仿真51单片机最小系统
查看>>
51 中断系统 外部中断0 外部中断1
查看>>
51 单片机 时间/计数器中断
查看>>
腾讯云本地还原mysql物理冷备
查看>>
算法图解 第1章 算法简介
查看>>
算法图解 第3章 递归
查看>>
Java反转整数
查看>>
解释 Zuul 的 zuul.strip-prefix 属性
查看>>
翻译 AbstractQueuedSynchronizer ( AQS )类注释
查看>>
HighCharts线型设定
查看>>
把win7 资源管理器的导航树改成xp的样式
查看>>
highcharts 内存泄露的解决
查看>>
blockUI 模态窗口
查看>>
网络通讯堵塞情况下的定时刷新
查看>>
手动将Apache注册为系统服务
查看>>