【Leetcode】【Medium】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"].
思考:
1、这是一道非常典型的求重复字符子串的问题,涉及到记录和查找,使用hash表最省时间,hash表需要key值取数字,自然考虑到将字符串转为ASCII码;
2、由于字母只有4种可能,那么直观的想到将A、C、G、T,分别对应:00,01,10,11,因此一个10个字母组成的子串,就可以表示为20个比特的int;
解题:
1、对原字符串进行遍历,每读一个字符,将其转为2位的bit值,并记录在int的对应位上;
2、读到10位之后,加入hash表中,并继续读取;当hash表中已经有对应数字存在,则为重复的字符串,记录在返回数组中;注意避免记录多次重复的字符串;
代码优化:
1、由于hash表只需记录当前值是否只出现一次,所以使用<int, bool>的键值对类型就可以了,bool变量true则只出现一次,出现其他次false;
2、虽然开始将ACGT转为00/01/10/11,但是发现重复后,不需要再转回来,因为字符串是顺序遍历的,遍历到重复后,包括当前遍历字符在内的前10个字符就是重复的子字符串;
3、每次ACGT转化,也会产生一定开销,由于ACGT的ASCII码值后三位分别为:001/011/111/100互不相同,而使用3个bit表示一个字母,10个字母30bit不会超过int的范围;因此使用三个bit来表示字母较好;
代码:
class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
unordered_map<int, bool> hmap;
vector<string> ret;
int dna_bit = ;
int bits_cut = 0x3FFFFFFF;
for (int i = ; i < s.length() && i < ; ++i) {
dna_bit = (dna_bit << ) | (s[i] & );
}
for (int i = ; i < s.length(); ++i) {
dna_bit = (dna_bit << ) | (s[i] & );
dna_bit = dna_bit & bits_cut;
if (hmap.find(dna_bit) != hmap.end()) {
if (hmap[dna_bit]) {
ret.push_back(s.substr(i - , ));
hmap[dna_bit] = false;
}
} else {
hmap[dna_bit] = true;
}
}
return ret;
}
};
另:
还可以将第一个循环去除,因为ACGT字母后三位都不全为0,所以如果字母不到10个在hash表中肯定是唯一的,如果hash表插入的开销很小,可以省略第一个循环,本文中没有省略;
附录:
C++ hash表高效使用
【Leetcode】【Medium】Repeated DNA Sequences的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- LeetCode 187. 重复的DNA序列(Repeated DNA Sequences)
187. 重复的DNA序列 187. Repeated DNA Sequences 题目描述 All DNA is composed of a series of nucleotides abbrev ...
- lc面试准备:Repeated DNA Sequences
1 题目 All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &quo ...
- 【LeetCode】Repeated DNA Sequences 解题报告
[题目] All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &quo ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【LeetCode】187. Repeated DNA Sequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/repeated ...
- 【leetcode】Repeated DNA Sequences(middle)★
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
随机推荐
- python any和all
摘自<流畅的Python> all 和 any 也是内置的归约函数. all(iterable) 如果 iterable 的每个元素都是真值,返回 True:all([]) 返回 True ...
- 使用virtualbox虚拟安装macos
需要工具: 虚拟机virtualbox:https://www.virtualbox.org/ empireEFIv1085.iso启动文件:http://yunpan.cn/c6UDGwL6wJm6 ...
- How to remove constantly launching services on Mac OS X
Even after you uninstall it, some Mac OS X software just won’t quit nagging you or notifying you of ...
- Docker搭建tomcat运行环境(修改镜像方式)
对于java程序员来说,要想使用Docker来部署你的应用,那么在镜像中安装类似于tomcat的容器基本上是必须的(sprintboot项目除外),本篇介绍自己基于对centos镜像的修改,创建自己的 ...
- windows下限制Redis端口只能由本机访问
在使用redis的时候,我只想要本机能够访问,这时可通过防火墙会阻止外界的访问 1.找到防火墙,选择高级设置2.点击"入站规则",再点击"新建规则" 3.点击& ...
- GPU体系架构(一):数据的并行处理
最近在了解GPU架构这方面的内容,由于资料零零散散,所以准备写两篇博客整理一下.GPU的架构复杂无比,这两篇文章也是从宏观的层面去一窥GPU的工作原理罢了 GPU根据厂商的不同,显卡型号的不同,GPU ...
- Oracle ORA-27102的解决办法(out of memory)
原文出自:https://blog.csdn.net/seesun2012 Oracle ORA-27102:out of memory 错误解决办法(简单粗暴,100%能解决内存占用问题) -前置: ...
- mybatis学习之动态sql
mybatis的动态sql语句很强大,在mapper映射文件中使用简单的标签即可实现该效果,下面一个个记录: 1.select查询 简单的select类似如下: <select id=" ...
- shell变量类型和运算符
一.shell变量的应用 1.shell变量的种类 ①用户自定义变量:由用户自己定义,修改和使用 ②预定义变量:bash预定义的特殊变量,不能直接修改 ③位置变量:通过命令行给程序传递执行参数 二.变 ...
- shell编程之export
shell 与 export命令用户登录到Linux系统后,系统将启动一个用户shell.在这个shell中,可以使用shell命令 或声明变量,也可以创建并运行shell脚本程序.运行shell脚本 ...