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的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

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

  4. LeetCode 187. 重复的DNA序列(Repeated DNA Sequences)

    187. 重复的DNA序列 187. Repeated DNA Sequences 题目描述 All DNA is composed of a series of nucleotides abbrev ...

  5. lc面试准备:Repeated DNA Sequences

    1 题目 All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &quo ...

  6. 【LeetCode】Repeated DNA Sequences 解题报告

    [题目] All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &quo ...

  7. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  8. 【LeetCode】187. Repeated DNA Sequences 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/repeated ...

  9. 【leetcode】Repeated DNA Sequences(middle)★

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

随机推荐

  1. Service启动流程

    Service启动流程从整个宏观上来看,它的模型如下 startService启动流程时序图 Activity中使用的startService方法是定义在Context的抽象类中,它的真正实现者是Co ...

  2. selenium 多窗口(windows)及ITargetLocator使用总结

    1. selenium能实现窗口切换的原理 2. 常见命令 2.1 WindowHandle 2.2 WindowHandles 2.3 SwitchTo 3. 使用JavaScript新建窗口 4. ...

  3. 修改Tomcat使用的JVM内存大小

    我的服务器的配置: # OS specific support.  $var _must_ be set to either true or false. JAVA_OPTS="-Xms10 ...

  4. 【javascript】onload load ready的那些事

    首先明确一下页面加载的步骤: 1.下载解析HTML文档结构 2.加载外部脚本文件与样式表文件 3.解析并执行脚本代码 4.构造HTML DOM模型 5 .加载图片等外部文件 6.页面加载完毕 接下来, ...

  5. JavaScript弹出层

    1.这个弹出层就是一个DIV 2.看需要什么效果 2.1.如果是仅仅需要弹出层,而背后的网页同样可以点击,那么只需要一个DIV即可,效果如图: 2.2.需要一层透明背景,而后面的网页只能看不能点,效果 ...

  6. 架构实战项目心得(四):使用Nexus配置Maven私有仓库

    一.安装配置Nexus 1.  下载nexus https://www.sonatype.com/download-oss-sonatype 2.  解压:tar -zxfnexus-3.5.2-01 ...

  7. spring-boot配置热更新

    spring boot使用的时候会遇到不断重启启动类的情况,热更新可以免去手动重启的烦恼,spring boot应用配置热更新主要有两种方式: 1.使用springloaded 配置pom.xml文件 ...

  8. 2017年11月20日 WinForm窗体 窗口无边框可移动&&窗口阴影 控制窗口关闭/最小化

    弹框 MessageBox.Show(); 清空 clear() 字符串拼接 string 公共控件 button 按钮 checkbox 复选框 checklistbox 多个复选框 combobo ...

  9. Error:Execution failed for task ':xutils:mergeDebugAndroidTestResources'. > No slave process to proc

    Error:Execution failed for task ':xutils:mergeDebugAndroidTestResources'. > No slave process to p ...

  10. json转换工具类

    using System;using System.Collections.Generic;using System.Text;using Newtonsoft.Json;using System.I ...