【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 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"].
提示:
这道题最初的想法就是使用HashMap来做,一开始的时候用了STL里的unordered_map:
class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
unordered_set<string> string_set;
if (s.length() <= ) {
return {};
}
unordered_map<string ,int> m;
for (int i = ; i < s.length() - ; ++i) {
string tmp = s.substr(i, );
if (m.find(tmp) == m.end()) {
m[tmp] = ;
} else {
string_set.insert(tmp);
}
}
return vector<string>(string_set.begin(), string_set.end());
}
};
做下来以后虽然AC了,但是时间却很不理想,原因也很简单,对于这种特定的问题,使用自定义的map和hash方法会让算法的效率得到很大的提高,之于如何设计map和hash的问题,我们不妨先看一看这个问题输入所具有的特点:
- 输入的字符串长度是固定的(10个字符)
- 字符只有{'A', 'C', 'T', 'G'}四种
- 结合上述两点,可以发现输入的可能性是可以计算出来的,即4^10个。
这样一来,我们就可以利用一个基本类型的数组作为map,而hash的算法则是可以想办法把4种字符分别映射到[1,4]这4个数字上,具体的做法可以看代码。
代码:
class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
// 将该char数组作为一个map
char map[] = {};
int len = s.length(), num = ;
vector<string> res;
if (len <= ) {
return res;
}
for (int i = ; i < ; ++i) {
num <<= ;
// 可以试一下下面的计算方法,效果就是把4个字符映射到了1,2,3,4这四个数字上
num |= (s[i] - 'A' + ) % ;
}
for (int i = ; i < s.length(); ++i) {
num <<= ;
num |= (s[i] - 'A' + ) % ;
// 0xfffff代表了20个1组成的二进制序列,按位与之后,结果就是当前字符串代表的数值
num = num & 0xfffff;
if (map[num]++ == ) {
res.push_back(s.substr(i - , ));
}
}
return res;
}
};
【LeetCode】187. Repeated DNA Sequences的更多相关文章
- 【LeetCode】187. Repeated DNA Sequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/repeated ...
- 【原创】leetCodeOj --- Repeated DNA Sequences 解题报告
原题地址: https://oj.leetcode.com/problems/repeated-dna-sequences/ 题目内容: All DNA is composed of a series ...
- [LeetCode] 187. Repeated DNA Sequences 求重复的DNA序列
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- leetcode 187. Repeated DNA Sequences 求重复的DNA串 ---------- java
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- Java for LeetCode 187 Repeated DNA Sequences
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- [LeetCode#187]Repeated DNA Sequences
Problem: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: ...
- [LeetCode] 187. Repeated DNA Sequences 解题思路
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- [leetcode]187. Repeated DNA Sequences寻找DNA中重复出现的子串
很重要的一道题 题型适合在面试的时候考 位操作和哈希表结合 public List<String> findRepeatedDnaSequences(String s) { /* 寻找出现 ...
- 187. Repeated DNA Sequences
题目: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: " ...
随机推荐
- 将github上的项目源码导入到eclipse中
1.注册github帐号 在github上注册一个自己的帐号. 2.安装git插件egit 在eclipse中安装git插件egit,安装方法可以参考这篇文章: http://www.cnblogs. ...
- Ionic集成ArcGIS JavaScript API.md
1. Ionic同原生ArcGIS JavaScript API结合 1.1. 安装esri-loader 在工程目录下命令行安装: npm install angular2-esri-loader ...
- JPA - hibernate 的各种常见用法
近期在做的项目使用的JPA,所以在此跟大家分享一下JPA的一些常见使用方法. JPA 是 JCP 组织发布的 Java EE 标准之一,因此任何声称符合 JPA 标准的框架都遵循同样的 ...
- 基于Spring的Appium配置应用
本文主要是讲述,使用Spring框架,优化Appium的Driver调用,并将写在代码里的大量配置参数定义到配置文件当中,还可灵活的控制调用AndroidDriver还是IOSDriver. Spri ...
- Day4-内置函数--未完待续,慢慢写
内置函数:https://docs.python.org/3/library/functions.html?highlight=built#ascii 未完待续....
- Advanced Sort Algorithms
1. Merge Sort public class Mergesort { private int[] numbers; private int[] helper; private int numb ...
- Automated Front End Test - Xvfb, Chromedriver, Selenium, Jenkins
1. Install Xvfbm, google-chrome-stable and chromedriver in Jenkins sudo apt-get install -y xvfb goog ...
- XML文件生成
import java.io.* ; import org.w3c.dom.* ; import javax.xml.parsers.* ; import javax.xml.transform.* ...
- html学习笔记 - sublime text 插件安装
command + shift + p 呼出搜索界面 输入 Packge Control:Install Package 进入到插件搜索列表 Emmet -- >快速生成html标签结构 Emm ...
- VR全景,零售业冬天里的一把火——全景智慧城市
对今天的中国来说,寻找经济转型的突破口,寻找经济权力的新霸主,零售业应该当仁不让. 零售业正在经历一场脱胎换骨的改造.一方面零售额达到前所未有的水平,另一方面,传统零售商也面临诸多挑战,其中之一,便是 ...