当前位置:网站首页>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/
边栏推荐
- Leetcode:829. 连续整数求和
- ECMAScript 6.0
- leetcode 1482 猜猜看啊,这道题目怎么二分?
- 小程序容器技术与物联网IoT的结合点
- [daily training] 1175 Prime permutation
- Take you through a circuit board, from design to production (dry goods)
- 4、【WebGIS实战】软件操作篇——数据导入及处理
- Learning notes for introduction to C language multithreaded programming
- Ultimate dolls 2.0 | encapsulation of cloud native delivery
- Cookie&Session
猜你喜欢

Ridge regression and lasso regression

完全背包问题
![[深度学习]激活函数(Sigmoid等)、前向传播、反向传播和梯度优化;optimizer.zero_grad(), loss.backward(), optimizer.step()的作用及原理](/img/9f/187ca83be1b88630a6c6fbfb0620ed.png)
[深度学习]激活函数(Sigmoid等)、前向传播、反向传播和梯度优化;optimizer.zero_grad(), loss.backward(), optimizer.step()的作用及原理

4、【WebGIS实战】软件操作篇——数据导入及处理

Nacos

The difference between MFC for static libraries and MFC for shared libraries

Use of comment keyword in database

E15 solution for cx5120 controlling Huichuan is620n servo error

The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)

Keil5中如何做到 0 Error(s), 0 Warning(s).
随机推荐
Learning notes for introduction to C language multithreaded programming
ASGNet论文和代码解读2
串口接收数据方案设计
【EI检索】2022年第六届材料工程与先进制造技术国际会议(MEAMT 2022)重要信息会议网址:www.meamt.org会议时间:2022年9月23-25日召开地点:中国南京截稿时间:2
jeecgboot输出日志,@Slf4j的使用方法
Use of comment keyword in database
Develop industrial Internet with the technical advantages of small programs
C语言的sem_t变量类型
FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)
JS daily development tips (continuous update)
torch.histc
Nacos
Take you through a circuit board, from design to production (dry goods)
详解Spark运行模式(local+standalone+yarn)
Pyramid Scene Parsing Network【PSPNet】论文阅读
复习专栏之---消息队列
【JPCS出版】2022年第三届控制理论与应用国际会议(ICoCTA 2022)
The shell script uses two bars to receive external parameters
网页不能右键 F12 查看源代码解决方案
Leetcode:829. 连续整数求和