【LeetCode】187. Repeated DNA Sequences 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址: https://leetcode.com/problems/repeated-dna-sequences/description/
题目描述:
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.
Example:
Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
Output: ["AAAAACCCCC", "CCCCCAAAAA"]
题目大意
在一个字符串中找出连续的十个字符,这十个字符不止在一个地方出现过。
解题方法
遍历+set。
做法简单了,需要一个长度是10的字符串切片,从头到尾把字符遍历一遍,然后不停的判断以这个位置开头的10个字符构成的字符串是否看到过,如果看到过就放到另外一个set里。为什么不直接放入list返回呢?因为一个字符串可能会重复多次,为了防止重复添加到结果里,必须set一下。
时间复杂度是O(N),空间复杂度是O(N)。
class Solution(object):
def findRepeatedDnaSequences(self, s):
"""
:type s: str
:rtype: List[str]
"""
seen = set()
repeated = set()
N = len(s)
for i in range(N):
cur = s[i : i+ 10]
if cur in seen:
repeated.add(cur)
else:
seen.add(cur)
return list(repeated)
参考资料:
https://leetcode.com/problems/repeated-dna-sequences/discuss/53855/7-lines-simple-Java-O(n)
日期
2018 年 10 月 11 日 —— 做Hard题真的很难
【LeetCode】187. Repeated DNA Sequences 解题报告(Python)的更多相关文章
- 【LeetCode】Repeated DNA Sequences 解题报告
[题目] All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &quo ...
- [LeetCode] 187. Repeated DNA Sequences 解题思路
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- [LeetCode] 187. Repeated DNA Sequences 求重复的DNA序列
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- Java for LeetCode 187 Repeated DNA Sequences
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- 【原创】leetCodeOj --- Repeated DNA Sequences 解题报告
原题地址: https://oj.leetcode.com/problems/repeated-dna-sequences/ 题目内容: All DNA is composed of a series ...
- leetcode 187. Repeated DNA Sequences 求重复的DNA串 ---------- java
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- [LeetCode#187]Repeated DNA Sequences
Problem: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: ...
- [leetcode]187. Repeated DNA Sequences寻找DNA中重复出现的子串
很重要的一道题 题型适合在面试的时候考 位操作和哈希表结合 public List<String> findRepeatedDnaSequences(String s) { /* 寻找出现 ...
- Leetcode OJ : Repeated DNA Sequences hash python solution
Total Accepted: 3790 Total Submissions: 21072 All DNA is composed of a series of nucleotides abb ...
随机推荐
- 【蛋白质基因组】Proteogenomics方法介绍及分析思路
概念 利用蛋白质组学数据,结合基因组数据(DNA).转录组数据(RNA)来研究基因组注释问题,被称为蛋白质基因组学."蛋白质基因组学"一词由Jaffe 等于2004 年首次提出,作 ...
- matplotlib 画饼图
有个瑕疵,某一块儿比例过小时,文字会重叠. 1 def pizza(data,labs,title): 2 import matplotlib 3 import matplotlib.pyplot a ...
- 03 Windows安装Java环境
Java环境安装 使用微信扫码关注微信公众号,并回复:"Java环境",免费获取下载链接! 1.卸载(电脑未装此程序,跳过此过程) 找到电脑上面的控制面板 找到这两个文 ...
- day02 web主流框架
day02 web主流框架 今日内容概要 手写简易版本web框架 借助于wsgiref模块 动静态网页 jinja2模板语法 前端.web框架.数据库三种结合 Python主流web框架 django ...
- 输入URL展示过程
一. 输入URL,回车 敲击某个键时,键盘内的处理器会先对键矩阵进行分析,然后将数据发送到计算机 计算机接收到来自键盘的信号,由键盘控制器(一种集成电路)进行处理,发送给操作系统 操作系统会分析,这些 ...
- NuxtJS的AsyncData和Fetch使用详解
asyncData 简介 asyncData 可以用来在客户端加载 Data 数据之前对其做一些处理,也可以在此发起异步请求,提前设置数据,这样在客户端加载页面的时候,就会直接加载提前渲染好并带有数据 ...
- 【编程思想】【设计模式】【测量模式Testability】Setter_injection
Python版 https://github.com/faif/python-patterns/blob/master/dft/setter_injection.py #!/usr/bin/pytho ...
- node.js require() 源码解读
时至今日,Node.js 的模块仓库 npmjs.com ,已经存放了15万个模块,其中绝大部分都是 CommonJS 格式.这种格式的核心就是 require 语句,模块通过它加载.学习 Node. ...
- 2.7 Rust Structs
A struct, or structure, is a custom data type that lets you name and package together multiple relat ...
- mybatis分页插件PageHelper源码浅析
PageHelper 是Mybaties中的一个分页插件.其maven坐标 <!-- https://mvnrepository.com/artifact/com.github.pagehelp ...