leetcode187. Repeated DNA Sequences
2016-01-26 15:12
483 查看
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了
第二种方法,存在字典中,如果在字典中出现且在ans中未曾录入的,则ans.append(),注意细节,range(len(s)-9)而不是-10
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
相关文章推荐
- 使用xib方式创建UITableViewCell,设置Label自动换行注意事项
- UIStatusBarStyle PreferredStatusBarStyle does not work
- 原生的UITableViewCell高度自适应,textLabel自动换行显示
- 使用ttXactAdmin、ttSQLCmdCacheInfo、ttSQLCmdQueryPlan获取SQL相关具体信息[TimesTen运维]
- request和response
- 300. Longest Increasing Subsequence
- IOS-49-UIActionSheet在iOS8后已废弃,以下为各版本通用适配办法
- LeetCode304. Range Sum Query 2D - Immutable
- UI方面总结
- php imagecreatetruecolor()方法报未定义错误解决方法
- iOS自定义UIButton的实现
- add "shadow+border+corner" specially for UIView
- UITextField输入框的字靠右一点显示
- poj-2299-Ultra-QuickSort(线段树 || 归并排序)
- UISegmentedControl 踩坑
- view 转为image 清晰度不变
- Label的文字自适应
- HDU 1242 Rescue (BFS or DFS)
- iPhone开发 – 小技巧二则 (UIColor 16进制颜色 | UIview设置frame)
- 解决iOS UITextField输入的内容在代理方面里面不能全部获取到