当前位置:网站首页>187. 重复的DNA序列
187. 重复的DNA序列
2022-07-01 03:23:00 【Sun_Sky_Sea】
187. 重复的DNA序列
原始题目链接:https://leetcode.cn/problems/repeated-dna-sequences/
DNA序列 由一系列核苷酸组成,缩写为 ‘A’, ‘C’, ‘G’ 和 ‘T’.。
例如,“ACGAATTCCG” 是一个 DNA序列 。
在研究 DNA 时,识别 DNA 中的重复序列非常有用。
给定一个表示 DNA序列 的字符串 s ,返回所有在 DNA 分子中出现不止一次的 长度为 10 的序列(子字符串)。你可以按 任意顺序 返回答案。
示例 1:
输入:s = “AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT”
输出:[“AAAAACCCCC”,“CCCCCAAAAA”]
示例 2:
输入:s = “AAAAAAAAAAAAA”
输出:[“AAAAAAAAAA”]
提示:
0 <= s.length <= 105
s[i]==‘A’、‘C’、‘G’ or ‘T’
解题思路:
遍历字符串s,从s的开头到len(s) - 10 + 1的索引长度,跨度是10,每次统计出现的次数,当等于2次的时候符合题意,大于2次的话再记录到答案列表中会出现重复,所以判断条件等于2次即可。
代码实现:
class Solution:
def findRepeatedDnaSequences(self, s: str) -> List[str]:
from collections import defaultdict
ans = []
# 用一个字典记录字符串出现的次数
# 使用int初始化
freq_dict = defaultdict(int)
# 遍历s,能索引的下标是从0到len(s) - 10 + 1
for i in range(len(s) - 9):
# 每次去i到i+10的跨度的子字符串
sub_s = s[i: i + 10]
# 统计次数
freq_dict[sub_s] += 1
# 题意要求不止一次,那么出现次数大于等于2次满足要求
# 大于2会插入重复值,或者使用set去重也可以
if freq_dict[sub_s] == 2:
ans.append(sub_s)
return ans
参考文献:
https://leetcode.cn/problems/repeated-dna-sequences/solution/zhong-fu-de-dnaxu-lie-by-leetcode-soluti-z8zn/
边栏推荐
- pytorch训练深度学习网络设置cuda指定的GPU可见
- 打包iso文件的话,怎样使用hybrid格式输出?isohybrid:command not found
- FCN全卷积网络理解及代码实现(来自pytorch官方实现)
- Appium自动化测试基础 — APPium基本原理
- Pyramid scene parsing network [pspnet] thesis reading
- [reach out to Party welfare] developer reload system sequence
- ECMAScript 6.0
- The preorder traversal of leetcode 144 binary tree and the expansion of leetcode 114 binary tree into a linked list
- Feign remote call and getaway gateway
- 报错:Plug-ins declaring extensions or extension points must set the singleton directive to true
猜你喜欢
后台系统右边内容如何出现滚动条和解决双滚动条的问题
jeecgboot输出日志,@Slf4j的使用方法
过滤器 Filter
报错:Plug-ins declaring extensions or extension points must set the singleton directive to true
后台系统页面左边菜单按钮和右边内容的处理,后台系统页面出现双滚动
Introduction to EtherCAT
Ultimate dolls 2.0 | encapsulation of cloud native delivery
文件上传下载
Bilinear upsampling and f.upsample in pytorch_ bilinear
岭回归和lasso回归
随机推荐
Unexpected token o in JSON at position 1 ,JSON解析问题
5、【WebGIS实战】软件操作篇——服务发布及权限管理
Blueprism registration, download and install -rpa Chapter 1
Idea plug-in backup table
【JPCS出版】2022年第三届控制理论与应用国际会议(ICoCTA 2022)
Asgnet paper and code interpretation 2
使用selenium自动化测试工具爬取高考相关院校专业招生分数线及排名情况
C语言的sem_t变量类型
数据交换 JSON
Pyramid Scene Parsing Network【PSPNet】论文阅读
Server rendering technology JSP
How to display scrollbars on the right side of the background system and how to solve the problem of double scrollbars
The combination of applet container technology and IOT
不用加减乘除实现加法
pytorch中的双线性插值上采样(Bilinear Upsampling)、F.upsample_bilinear
还在浪费脑细胞自学吗,这份面试笔记绝对是C站天花板
MFC窗口滚动条用法
Listener listener
Test function in pychram
报错:Plug-ins declaring extensions or extension points must set the singleton directive to true