Leetcode Repeated DNA Sequences

【LeetCode】Repeated DNA Sequences 解题报告 【题目】 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 D 阅读详情

Leetcode Repeated DNA Sequences

今天突然看到Leetcode上出了一个新题, 就立刻着手试试;

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.

For example

Given s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT",
Return:
["AAAAACCCCC", "CCCCCAAAAA"].

题意: 所有DNA都是由一系列碱基构成, 分别为ACGT, 题目要求找出所有长度为10的子串, 这些子串在原串中出现次数必须大于1次(重复出现);



思路历程:

-- 这是一个子串对自身匹配的问题;

-- 第一反应有3种方法: 其一, 使用hash(c++11的unordered_set)来存储所有长度为10的子串; 具体步骤是构造unordered_set<string> repeated, 遍历输入的原串, 对s[i]到s[i+9]的序列构成的子串, 如未出现在repeated中, 则存入repeated; 如出现在repeated中, 则说明该子串曾出现过, 符合题意要求, 将其存入答案vector<string> answer;

当然, 这个代码最后是memory limit exceeded; 先看代码, 后面分析;


class Solution {
public:
    vector<string> findRepeatedDnaSequences(string s) {
    	vector<string> answer;
        unordered_set<string> repeated;
        for (int i = 0; i + 9 < s.size(); ++i) {
        	string t(s, i, 10);
        	unordered_set<string>::iterator it = repeated.find(t);
        	if (it != repeated.end())
        		answer.push_back(t);//在repeated中查找成功, 说明曾出现过该子串, 则存入answer
        	else
        		repeated.insert(t);//repeated中查找失败, 说明未曾出现过, 则存入repeated
        }
        return answer;
    }
};//memory limit exceeded

分析:

时间复杂度是O(n), 其中n为原串长度;

MLE的可能原因: unordered_set<string>对于超长的输入串, 会消耗大量的存储空间; 另外, 上面代码并未考虑answer中的重复答案, 因为每次只要出现在repeated中就放入answer, 这显然有问题;


改进方法:

-- 由于碱基无非ACGT四种类型, 可以使用00 01 10 11四个状态代替ACGT四种碱基, 比如AAGCT就是00 00 10 01 11, 对任意一个长度为10的子串都可以转化使用20个位的int值hint; 这样就可将unordered_set<string> repeated转变为unordered_set<int> repeated, 一定程度上减少了所需的存储空间;

-- 对于如何去重, 其一可以先收集所有答案, 再sort, unique去重, 当然这样很慢也很麻烦; 其二, 可以再构造一个unordered_set<int> check, 用于存储已经存入answer中的重复子串对应的hint值;

-- 值得一提的是, 每次从s[i]->s[i+9]变为s[i+1]->s[i+10], 使用了这样一个方法:

hint = ((hint & eraser) << 2) + ati[s[i]];
其中eraser是一个宏定义, 值为0x3ffff, 二进制是00111111111111111111, 用于擦除hint中的最高2位s[i]碱基对应的值, 再左移2, 最后加上s[i+10]的碱基对应的值;

代码如下:

class Solution {
public:
	#define eraser 0x3ffff
	vector<string> findRepeatedDnaSequences(string s) {
	    vector<string> answer;
	    int hint = 0;//存储长度为10的子串翻译后的int值
	    if (s.size() < 10)
	    	return answer;
	    unordered_set<unsigned int> repeated, check;//repeated存储已出现的子串, check用于防止重复答案
	    unordered_map<char, unsigned int> ati{{'A', 0}, {'C', 1}, {'G', 2}, {'T', 3}};//此处ati是存储各碱基对应的值00 01 10 11(c++11新语法)
	    for (int i = 0; i != 10; ++i) {
	    	hint = (hint << 2) + ati[s[i]];//用s的前10个碱基构造初始hint值
	    }
	    repeated.insert(hint);
	    for (int i = 10; i != s.size(); ++i) {
	    	hint = ((hint & eraser) << 2) + ati[s[i]]; //子串变化对应hint值变化
	    	unordered_set<unsigned int>::iterator it = repeated.find(hint);
	    	if (it != repeated.end()) {
	    		it = check.find(hint);
	    		if (it == check.end()) {
	    			answer.push_back(string(s, i - 9, 10));
	    			check.insert(hint);
	    		}
	    	}
	    	else
	    		repeated.insert(hint);
	    }
	    return answer;
	}
};
分析:

一开始由于忽略了移位与其他运算符的优先级关系, 一直出问题, 后来才发现

hint = ((hint & eraser) << 2) + ati[s[i]];
里面的外面那层括号没加上, 浪费了不少时间, 确实不应该


最后:

-- 在代码中用到了unordered_set和unordered_map等散列容器, 不知道是否有O(n)算法可以不适用这些方法, 如果有请大家告诉我;

-- 一开始其实还有一种想法, 就是枚举每个长度为10的子串, 使用KMP算法, 时间复杂度应该是O(n2), 但占用的存储空间为O(1), 不知道效果会怎么样, 如果有朋友试过, 也请与我讨论一下~

VTK:vtkAssembly用法实战 VTK:vtkAssembly用法实战程序输出程序完整源代码 程序输出 程序完整源代码 #include <vtkActor.h> #include <vtkAssembly.h> #include <vtkAxesActor.h> #include <vtkCubeSource.h> #include <vtkNamedColors.h> #include <vtkNew.h> #include <vtkOrientation 阅读详情

相关推荐

别再只会用Kriging了!用Python+GeoPandas手把手实现IDW插值(附完整代码与可视化)

本文详细介绍了如何使用Python和GeoPandas实现反距离加权插值(IDW),包括核心算法、参数调优和可视化技巧。通过完整代码示例,帮助读者掌握这一空间数据分析技术,适用于环境监测、气象预报等多个领域,提升数据处理效率和灵活性。

weixin_30872157的博客 225

LeetCode 187. Repeated DNA Sequences 重复的DNA序列

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...

ConnieWu的博客 970

OSMDroid 切换地图不更新的问题

用OSMDroid加载Google地图的时候 需要有按钮切换不同图层的地图,如果发现调用了mMapView.setTileSource(ITileSource aTileSource)方法的时候发现地图View没有更新的话,可能是因为两个实例化TileSource对象其子类BitmapTileSourceBase这个类的aName成员变量值一样导致的地图在setTileSource出现不更新问题,...

godoge的博客 1039

187. Repeated DNA Sequences

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. Wri

学习应该是一个长链接 439

ubuntu20.04下内核源码下载分析

ubuntu 20.04下内核源代码下载,配置编译环境写在前面一、安装VMware Workstation和Ubuntu20.04二、修改apt源合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出导入 写在前面 我最近的科研工作需要使用到对ubun

冰冻三尺非一日之寒 1万+

基于FDTD的涡旋光束发射器设计

为实现涡旋光束在小型设备中的应用,提出一种圆柱孔光栅蚀刻硅微环谐振腔结构,器件半径仅为4.1μm。建立一个理论模型,利用偶极子点表示沿微环腔内壁的光栅元件散射辐射,推导输出光场表达式,建模结果验证该结构可在不同波长下选择性地产生各种拓扑数的涡旋光束。此外,对器件进行性能分析与参数优化,结果表明,微环波导宽度过小会大大增加微环的辐射损耗。当微环波导宽度为450nm、耦合间距为100nm时,1 579nm波长附近的谐振峰输出功率为16.3dB,Q值为15 800。

qq_61890005的博客 827

187. Repeated DNA Sequences**

187. Repeated DNA Sequences** https://leetcode.com/problems/repeated-dna-sequences/ 题目描述 All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When s...

珍妮的选择的博客 279

【红外DDE算法】一种应用于高动态范围红外可视化的算法

【红外DDE算法】一种应用于高动态范围红外可视化的算法 - 这个算法背后的动机 - 展示这个算法的流程 - 然后讨论各个可调参数的意义。

sunwindl91181的博客 1682

Repeated DNA Sequences —— Leetcode(教训,重做)

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. Wri

BlitzSkies的专栏 557

android 14.0 去掉usb授权提示框 默认给予权限

在14.0的系统rom产品开发中,在进行iot关于插入usb设备的开发过程中,在插入usb设备时会弹出usb授权提示框,也带来一些不便, 这个需要默认授予USB权限,插拔usb都不弹出usb弹窗所以这要从usb授权相关管理页默认给与usb权限,接下来分析下 相关的实现功能流程

安卓兼职framework和app工程师的博客 2261

代谢组数据分析(三十一):空间代谢组学分析从数据加载到通路分析的流程解析

空间代谢组学是一种先进的技术,能够在组织切片上以高分辨率检测代谢物分布,为理解生物代谢过程提供新视角。其分析流程包括数据获取、预处理、代谢物注释、差异表达分析、可视化、通路分析、聚类分析及空间相关代谢物分析等关键步骤。这些步骤帮助研究人员从海量数据中提取有价值信息,揭示代谢物的空间分布规律,发现潜在生物标志物,探索代谢通路调控机制。例如,SpaMTP等工具为全流程分析提供了强大支持,助力研究人员高效处理数据,深入理解代谢过程,为生物学研究和临床应用提供重要依据。

专注生信领域 918

[leetcode] Repeated DNA Sequences

题目链接https://leetcode.com/problems/repeated-dna-sequences/ 题意:寻找出现1次以上的长度为10的基因片段,例如: 需要特别注意的是:所有基因均由 A, C, G, T 四种核苷酸组成。 看到题目,最先想到的是提取所有的长度为10的片段,放入map中,map的key为长度为10的片段,value为key出现的起始位置。提交之

曲径通幽处 464

基于STM32开发板的USB MIDI键盘实现

在本文中,我们介绍了如何使用STM32开发板和相应的软件工具来实现USB MIDI键盘。通过配置STM32CubeMX和编写相应的源代码,我们能够将STM32开发板转换为一个功能齐全的USB MIDI设备。我们将使用STM32开发板和相应的软件工具来实现这一功能,并提供相应的源代码供参考。此时,您应该可以使用STM32开发板作为USB MIDI键盘,并在按键按下时听到相应的音符。在STM32CubeMX中,选择适当的STM32型号,并启动一个新的项目。然后,将生成的可执行文件烧录到STM32开发板中。

2301_79326616的博客 1588

Repeated DNA Sequences [leetcode]

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. Wri

Parrot的专栏 1417

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

leetcode 187: Repeated DNA Sequences

Total Accepted: 1161 Total Submissions: 6887 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 usef

西施豆腐渣 6033

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

Leetcode161: Repeated DNA Sequences

原文地址 : https://so.youkuaiyun.com/so/search/s.do?q=leetcode161&t=%20&u= All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it...

Umbrella Corporation 9627

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

【Leetcode】Repeated DNA Sequences

题目链接:https://leetcode.com/problems/repeated-dna-sequences/题目: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: “ACGAATTCCG”. When studying DNA, it is sometim

业精于勤,荒于嬉 3197

LeetCode Repeated DNA Sequences

LeetCode Repeated DNA Sequences 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 repe

FightForProgrammer的专栏 831

LeetCode-Repeated DNA Sequences

题目链接:https://leetcode.com/problems/repeated-dna-sequences/ All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is someti

vanish_dust 569

Leetcode:Repeated DNA Sequences

转自:http://www.tuicool.com/articles/AnuQJjA hash table plus bit manipulation method (view the Show Tags and Runtime 10ms !) 算法分析 首先考虑将ACGT进行二进制编码 A -> 00 C -> 01 G -> 10 T -

kkForWork的专栏 749

[leetcode] 187 Repeated DNA Sequences

(一)最一开始的做法是使用 map 记录每个10个字符的字符串的个数,超过2就push_back进ans。但是MLE了,说明采用string并不是一个好方法。 下面是MLE的代码: class Solution { public: vector findRepeatedDnaSequences(string s) { vector ans; map mp;

NK_test的博客 2826

[LeetCode]Repeated DNA Sequences

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. Wri

大力拜 841

leetcode之 Repeated DNA Sequences

Total Accepted: 2932 Total Submissions: 16668All 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 identi

TO TOP CODER 698

LeetCode: 187. Repeated DNA Sequences

LeetCode: 187. Repeated DNA Sequences 解题思路 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 ide...

杨领well的专栏 192

Leetcode之Repeated DNA Sequences

题目: 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. ...

qq_35455503的博客 135

Leetcode:Repeated DNA Sequences

开始看到题目,觉得就是直接从第一个子字符串开始遍历,并存储在List中,如果某个子字符串出现两次,就将其添加到结果列表中。结果TLE(Time Limited Exceeded)了。代码如下: public class Solution { public List findRepeatedDnaSequences(String s) { List res

redstraw的专栏 2577
上一篇: 二叉树与森林的转换
下一篇: AC自动机模板及对多模式匹配的理解
coderhuhy
博客等级 码龄12年 24粉丝 40原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值