当前位置:网站首页>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/
边栏推荐
- Nacos
- Pathmeasure implements loading animation
- Depth first traversal of C implementation Diagram -- non recursive code
- Appium自动化测试基础--补充:C/S架构和B/S架构说明
- TEC: Knowledge Graph Embedding with Triple Context
- 后台系统页面左边菜单按钮和右边内容的处理,后台系统页面出现双滚动
- torch. histc
- LeetCode 144二叉树的前序遍历、LeetCode 114二叉树展开为链表
- Avalanche problem and the use of sentinel
- Complete knapsack problem
猜你喜欢

Asgnet paper and code interpretation 2

Appium自动化测试基础 — APPium基本原理

小程序容器技术与物联网IoT的结合点

Develop industrial Internet with the technical advantages of small programs

完全背包问题
![5. [WebGIS practice] software operation - service release and permission management](/img/5d/070e207bd96e60ba1846d644d4fb54.png)
5. [WebGIS practice] software operation - service release and permission management

Unexpected token o in JSON at position 1 ,JSON解析问题

IPv4 and IPv6, LAN and WAN, gateway, public IP and private IP, IP address, subnet mask, network segment, network number, host number, network address, host address, and IP segment / number - what does

BluePrism注册下载并安装-RPA第一章

服务器渲染技术jsp
随机推荐
【EI会议】2022年国际土木与海洋工程联合会议(JCCME 2022)
File upload and download
完全背包问题
小程序容器技术与物联网IoT的结合点
Edge drawing: a combined real-time edge and segment detector
Feature Pyramid Networks for Object Detection论文理解
监听器 Listener
pytorch训练深度学习网络设置cuda指定的GPU可见
The combination of applet container technology and IOT
Cygwin的下载和安装配置
4. [WebGIS practice] software operation chapter - data import and processing
Pytorch training deep learning network settings CUDA specified GPU visible
Avalanche problem and the use of sentinel
还在浪费脑细胞自学吗,这份面试笔记绝对是C站天花板
RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
[小样本分割]论文解读Prior Guided Feature Enrichment Network for Few-Shot Segmentation
Unexpected token o in JSON at position 1 ,JSON解析问题
Feature pyramid networks for object detection
JS daily development tips (continuous update)
Edlines: a real time line segment detector with a false detection control