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

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

根据题目意思,很容易想到最直接的办法:取所有可能的子串,逐个比较。
<span style="white-space:pre">	</span>vector<string> res;
	if (s.length() <=10)
	{
		return res;
	}

	int len = s.length();
	for (int i = 0; i < len - 10; i++)
	{
		string str1 = s.substr(i, 10);
		for (int j = i + 1; j < len - 10; j++)
		{
			string str2 = s.substr(j, 10);
			if (str1 == str2)
			{
				res.push_back(str1);
				break;
			}
		}
	}

	return res;
时间复杂度O(n^2),结果很明显:超时。

这时想到另外一个办法,用空间换时间:哈希。将所有可能出现的字符序列保存在map<string,int> table中(其中第一个字段表示出现的子串,第二个字段表示此子串出现的次数),然后对map遍历,如果table中第二个字段>1表示此子串有重复,加入返回数组中。
<span style="white-space:pre">	</span>vector<string> res;
	if (s.length() < 11)
		return res;

	int len = s.length();
	map<string, int> table;
	for (int i = 0; i < len - 10; i++)
	{
		string str = s.substr(i, 10);
		if (table.find(str) != table.end())
		{
			table[str]++;
		}
		else
		{
			table[str] = 1;
		}
	}

	for (map<string, int>::iterator it = table.begin(); it != table.end(); ++it)
	{
		if (it->second > 1)
			res.push_back(it->first);
	}

	return res;
结果:超内存。。。

超内存的原因是什么?肯定是table中的第一个字段,它是字符串,肯定需要很多空间。能不能换个思维,把字符串哈希一下,哈希成可以用比较小的空间就能表示的?因为可能出现的字符只有四种:AGCT,那么这么设计哈希函数:A->0,C->1,G->2,T->3。如此一来,把长度为10的字符串映射(哈希)成整数。
int hash_fun1(string s)
{
	int n = 0;
	for (int i = 0; i < s.length(); i++){
		n <<= 2;
		char c = s[i];
		if (c == 'C'){
			n += 1;
		}
		else if (c == 'G'){
			n += 2;
		}
		else if (c == 'T'){
			n += 3;
		}
	}
	return n;
}

vector<string> findRepeatedDnaSequences(string s) 
{
	vector<string> res;
	if (s.length() < 11)
		return res;

	int len = s.length();
	map<int, int> table;
	for (int i = 0; i <= len - 10; i++)
	{
		string str = s.substr(i, 10);
		int val = hash_fun1(str);//把字符串哈希成int
		map<int, int>::iterator it = table.find(val);

		if (it != table.end())
		{
			if (it->second == 1)//出现过一次,则加入
				res.push_back(str);
			
				it->second++;
		}
		else
		{
			table[val] = 1;
		}
	}

	return res;
}
当然还有其他哈希办法。比如网上一位朋友的代码,利用位运算(AGCT后3为的位各不相同)
int str2int(string s) {
    int str=0;
    for (int i = 0; i < s.size(); ++i)
        str = (str<<3) +(s[i]&7);
    return str;
}
vector<string> findRepeatedDnaSequences(string s) {
    vector<string> res;
    unordered_map<int,int> coll;
    for (int i = 0; i+10 <= s.size(); ++i)
        if (coll[str2int(s.substr(i,10))]++ == 1)
            res.push_back(s.substr(i,10));
    return res;
}




Cisco Firepower FMCv修改管理Ip方法 阅读详情

相关推荐

实战|验证码突破思路

漏洞成因:由于逻辑设计缺陷,可绕过验证,比如直接删除COOKIE或验证码参数可绕过、当验证不通过清空session时,验证码参数值为空时绕过等。测试方法:抓包,删除验证码字段,查看是否可以成功发送抓包,正常流程下,记录验证码后的数据包,替换目标包中内容,直接发送,查看是否可以直接绕过验证码案例:Step1.输入正确账户信息和错误验证码,登录时抓包Step2.删除COOKIEStep3.客户端登陆成功。

weixin_46239998的博客 8098

Leetcode Repeated DNA Sequences

Leetcode Repeated DNA Sequences

coderhuhy的专栏 4012

从宁德新能源面试官视角,拆解Halcon/OpenCV图像处理工程师的硬核技能树(附避坑指南)

本文从宁德新能源面试官视角出发,详细拆解Halcon/OpenCV图像处理工程师的核心技能树,涵盖硬件选型、算法实现和系统工程思维三大能力维度。文章提供工业视觉工程师面试的实战避坑指南,包括硬件配置要点、算法优化技巧及职业发展路径,助力求职者系统提升图像处理专业技能。

weixin_28732953的博客 127

[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

Qualcomm平台下基于QSEE的指纹识别方案移植之四TA移植

本文只介绍TA移植的过程,不做技术分析,关于TA开发另做记录。 一、TA介绍 TA,Trusted application 可信应用,是运行于QSEE中的可执行程序。 指纹TA文件如下,形式一般是 xxx.mbn; xxx.b0 ~ xxx.b6, xxx.flist, xxx.mdt; xxx.mbn 可以理解为其它几个文件的集合,签名的时候用的就是这个文件,签完名之后,有对应的工具将它...

梦路遥 7548

医疗器械软件测试输出内容(1)

*单元测试(Unit Testing):**单元测试是软件测试的最底层,用于验证软件中最小的可测试单元——函数、方法或模块。系统测试是在集成测试之后进行的,它对整个系统进行测试,验证系统在不同部分集成在一起后是否满足需求和规格。: 集成测试是在单元测试之后进行的,它关注不同单元之间的交互和集成。对于不同安全级别的软件(IEC 62304 A类、B类、C类),在体系上要求输出的文件内容有所区别。**说明:**将集成测试和软件系统测试合并为一项单一的计划和一组活动是可接收的。

ct745363083的博客 1867

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

手把手教你用高频注入法辨识永磁同步电机电感参数(附MATLAB仿真)

本文详细介绍了高频注入法在永磁同步电机电感参数辨识中的工程实践与MATLAB仿真指南。通过高频信号注入法,无需拆解电机即可在线精确测量Ld和Lq参数,适用于电动汽车驱动和工业伺服等高精度控制场景。文章包含信号设计、硬件实现、解调算法及完整MATLAB仿真模型构建,帮助工程师快速掌握这一关键技术。

orange的博客 292

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

代码生成 | 安时积分法模型搭建

安时积分法是电池电量计量最基础的方法,今天我们用simulink建模的方式做一个安时积分模型,从而更好地理解安时积分的思想也掌握建模的基础操 ​新建文件 打开MATLAB启动simulink新建一个模型文件 定义变量 和手写代码一样,先定义几个后面要用到的变量,在simulink建模时新建变量用Data Store Memory模块。双击填入要定义的变量名,然后在signal attributes赋初值 如此方法新建四个变量current(电流)初值1000mA、tatol_cap(总容量

ningvay的博客 1万+

【STK】手把手教你利用STK进行导弹和反导仿真04 - STK/MMT模块03 导弹飞行工具应用案例

飞行设置的工具栏就在附加窗口的中间,首先点击左边的General,修改这个项目的名字。然后在下拉的System中选择导弹的类型,这里我们选择远程。(4)输出数据文件到STK,任务窗口里选择Export Manager项,飞行数据输出到STK。项目可以命名为Test;(1)首先建立新项目,点击Flight菜单中的New Flight选项,见下图。首先看世界地图中间的红圈,是一个导弹形状的图标,这个图标表示的是导弹的发射点。选择下面的Export Manager,依次勾选红框的选项。我们拖动这个导弹的图标,

weixin_44184852的博客 1630

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

微信小程序 - 实现大文件上传分片/切片+本地选择文件上传超大文件,小程序大文件切片上传+断点续传+上传进度条+失败自动重传完整运行示例源码,支持图片、视频、文档各种文件(支持上传单个文件或批量上传)

微信小程序实现大文件上传,实现分片大文件上传,微信小程序 分片 断点续传 大文件上传,微信小程序实现大文件上传 分片上传 进度条,小程序文件分片上传,微信小程序如何上传很大的文件,在选择文件后,按指定大小将文件切割成独立的文件片,并逐片上传,uniapp 微信小程序上传大文件如何切片,微信小程序实现大文件分片上传、断点续传、秒传,解决大文件上传问题,大文件上传如何做断点续传,微信小程序wx.uploadFile,小程序上传视频,小程序上传文档,实现大文件上传(切片上传)实现大文件上传到服务器教程,支持批量多

🏆 前端领域优质创作者 1万+

【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

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

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

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

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

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

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

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

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

dyllanzhou的专栏 317

Leetcode:Repeated DNA Sequences

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

redstraw的专栏 2577

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

weixin_34130389的博客 74

LeetCode || Repeated DNA Sequences

Repeated DNA Sequences  Total Accepted: 19705 Total Submissions: 99010My Submissions Question  Solution  All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T,

xuxumoi 697
上一篇: LeetCode Rotate Array
下一篇: LeetCode Min Stack
FightForProgrammer
博客等级 码龄13年 239粉丝 75原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值