【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: " ...
随机推荐
- 通过winform+模拟登录实现快速一键登录到人才招聘网站
之前为了便于人事部门招聘登录网站更简洁高效,免去每天频繁输网址.用户名.密码等相关登录信息,特基于winform+HttpWebRequest实现模拟请求登录,最终达到一键登录到招聘网站后台的效果. ...
- 通过分析 JDK 源代码研究 TreeMap 红黑树算法实
TreeMap和TreeSet是Java Collection Framework的两个重要成员,其中TreeMap是Map接口的常用实现类,而TreeSet是Set接口的常用实现类.虽然HashMa ...
- jQuery之文档处理
jQuery 文档处理 1)内部插入 2)外部插入 3)包裹 4)替换 5)删除 6)复制 1.内部插入 append(content|fn) 向每个匹配的元素内部追加内容. 向所有段落中追加一些HT ...
- gitlab 添加SSH Key
1.登录http://domain/users/sign_in 2.选择"Profile Settings",进入"Profile Settings"设置页面 ...
- js实用方法记录-简单cookie操作
js实用方法记录-简单cookie操作 设置cookie:setCookie(名称,值,保存时间,保存域); 获取cookie:setCookie(名称); 移除cookie:setCookie(名称 ...
- 使用DFA算法对敏感词进行过滤
项目目录结构如下: 其中resources资源目录中: stopwd.txt :停顿词,匹配时间直接过滤. wd.txt:敏感词库. 1.WordFilter敏感词过滤类: package com.s ...
- 关于微信小程序遇到的wx.request({})问题
域名请求错误问题 当我们在编写小程序,要发送请求时,wx.request({})时或许会遇到如下的问题: 一:这是因为微信小程序的开发中,域名只能是https方式请求,所以我们必须在小程序微信公众平台 ...
- MySQL中间件Atlas安装及使用
简介 Atlas是由 Qihoo 360公司Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目.它在MySQL官方推出的MySQL-Proxy 0.8.2版本的基础上,修改了大量 ...
- 为什么各大厂商要抢先跟进H.265?
继爱奇艺.乐视等视频厂商宣布支持 H.265 高清视频后,2014 年 4 月,搜狐视频宣布正式上线视频行业首个 H.265 高清大片专区,可在线观看 200 余部当下最火的超高清大片.国外 BBC ...
- fidder从基础到熟练
一.fidder介绍 1.Fiddler是一款由C#语言开发的免费http调试代理软件,有.net 2 和 .net 4 两种版本.Fiddler能够记录所有的你电脑和互联网之间的http通讯,Fid ...