【原创】leetCodeOj --- Repeated DNA Sequences 解题报告
原题地址:
https://oj.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 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"].
方法:
大概的方向是,遍历所有长度为10的子串,用一个hash表记录每个不同子串的出现次数,最后输出满足条件的子串。
关键问题是:如何更快
我的办法:将A、C、G、T映射到1、2、3、4,然后换算成大整数。为了方便计算,字符串的最左边是最低位。这么说有些语焉不详,举几个例子:
AACG = 4311
CGTA = 1432
然后计算出首个字符串的整数值并加入map,这样,每下一个子串都可以通过 整数值/10 + 下一个字符乘以十亿来得到。
这样,hash值的计算从字符串变成了整数,同时,获得下一个字符串的行为也可以在更快的常数次时间内完成,因为操作字符串的时间开支。
全部代码:
class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
unordered_map<long long,int> dict;
unordered_map<long long,int> :: iterator it;
vector<string> res;
long long flag = 1000000000;
if (s.size() <= 10)
return res;
long long num = generateFirstNum(s);
dict[num] = 1;
for (int i = 10; i < s.size(); i ++) {
num /= 10;
long long now = getCharNum(s[i]);
num += now * flag;
it = dict.find(num);
if (it == dict.end()) {
dict[num] = 1;
} else {
dict[num] += 1;
}
}
for (it = dict.begin(); it != dict.end(); it ++) {
if (it->second > 1) {
generateRes(res,it->first);
}
}
return res;
} long long generateFirstNum(string s) {
long long res = 0;
long long power = 1;
for (int i = 0; i < 10; i ++) {
long long num = getCharNum(s[i]);
res += num * power;
power *= 10;
}
return res;
} long long getCharNum(char s) {
switch (s) {
case 'A' : return 1;
case 'C' : return 2;
case 'G' : return 3;
case 'T' : return 4;
}
} char getNumChar(long long s) {
switch (s) {
case 1 : return 'A';
case 2 : return 'C';
case 3 : return 'G';
case 4 : return 'T';
}
} void generateRes(vector<string> &res,long long target) {
string s;
while (target > 0) {
char now = getNumChar(target % 10);
s = s + now;
target /= 10;
}
res.push_back(s);
}
};
【原创】leetCodeOj --- Repeated DNA Sequences 解题报告的更多相关文章
- 【LeetCode】187. Repeated DNA Sequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/repeated ...
- 【LeetCode】Repeated DNA Sequences 解题报告
[题目] All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &quo ...
- [LeetCode] 187. Repeated DNA Sequences 解题思路
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- 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 187. 重复的DNA序列(Repeated DNA Sequences)
187. 重复的DNA序列 187. Repeated DNA Sequences 题目描述 All DNA is composed of a series of nucleotides abbrev ...
- 【原创】leetCodeOj --- Sliding Window Maximum 解题报告
天,这题我已经没有底气高呼“水”了... 题目的地址: https://leetcode.com/problems/sliding-window-maximum/ 题目内容: Given an arr ...
- [Swift]LeetCode187. 重复的DNA序列 | Repeated DNA Sequences
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- 【原创】leetCodeOj --- Word Ladder II 解题报告 (迄今为止最痛苦的一道题)
原题地址: https://oj.leetcode.com/submissions/detail/19446353/ 题目内容: Given two words (start and end), an ...
- 【原创】leetCodeOj --- Factorial Trailing Zeroes 解题报告
原题地址: https://oj.leetcode.com/problems/factorial-trailing-zeroes/ 题目内容: Given an integer n, return t ...
随机推荐
- sqlserver 操作技巧
1.将不同库中的一张表数据导入到另外一张表中去 ① 两张表多存在实体,两表的字段相同,字段的顺序相同的话. insert into 表B select * from 表A ② 两张表多存在实体,两表的 ...
- 奔小康赚大钱 hdu 2255
奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- qt 获取windows 的消息(通过MFC的DLL的透明窗体转发消息)good
qt 给win32 发送消息很简单,但是要获取windows 消息却十分复杂,最后想了一个不是很完美 但是也是以现在本人能力所能实现的唯一途径了,基本原理是 利用vc编写一个mfc 的dll ,这个d ...
- [Android]ADT Run时候报错:The connection to adb is down, and a severe error has occured
The connection to adb is down, and a severe error has occured. 之 ..\sdk\platform-tools\adb.exe and c ...
- Java创建、重命名、删除文件和文件夹(转)
Java的文件操作太基础,缺乏很多实用工具,比如对目录的操作,支持就非常的差了.如果你经常用Java操作文件或文件夹,你会觉得反复编写这些代码是令人沮丧的问题,而且要大量用到递归. 下面是的一个解决方 ...
- Android开源项目大全 - 工具类
主要包括那些不错的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容.多媒体相关及其他. 一.依赖注入DI 通过依赖注入减少Vie ...
- hdu4059 The Boss on Mars
The Boss on Mars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Android推断程序前后台状态
public class AppStatusService extends Service { private static final String TAG = "AppStatusSer ...
- Java程序猿之从菜鸟到职场高手的必看
J2SE之入门引导 Java基础系列之初识JAVA Java基础系列之Java语法 ...
- Android自己定义组件系列【2】——Scroller类
在上一篇中介绍了View类的scrollTo和scrollBy两个方法,对这两个方法不太了解的朋友能够先看<自己定义View及ViewGroup> scrollTo和scrollBy尽管实 ...