原题地址:

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 解题报告的更多相关文章

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

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

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

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

  3. [LeetCode] 187. Repeated DNA Sequences 解题思路

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

  4. lc面试准备:Repeated DNA Sequences

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

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

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

  6. 【原创】leetCodeOj --- Sliding Window Maximum 解题报告

    天,这题我已经没有底气高呼“水”了... 题目的地址: https://leetcode.com/problems/sliding-window-maximum/ 题目内容: Given an arr ...

  7. [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 ...

  8. 【原创】leetCodeOj --- Word Ladder II 解题报告 (迄今为止最痛苦的一道题)

    原题地址: https://oj.leetcode.com/submissions/detail/19446353/ 题目内容: Given two words (start and end), an ...

  9. 【原创】leetCodeOj --- Factorial Trailing Zeroes 解题报告

    原题地址: https://oj.leetcode.com/problems/factorial-trailing-zeroes/ 题目内容: Given an integer n, return t ...

随机推荐

  1. jQuery选择

    1.基本的选择 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY29tZW9uc3RvbmU=/font/5a6L5L2T/fontsize/400/fil ...

  2. Delphi数据类型转换(有几个字符串函数没见过,比如StringToWideChar和WideCharToString)

    DateTimeToFileDate                  函数                     将DELPHI的日期格式转换为DOS的日期格式         DateTimeT ...

  3. Thinkphp框架拓展包使用方式详细介绍--验证码实例(十一)

    原文:Thinkphp框架拓展包使用方式详细介绍--验证码实例(十一) 拓展压缩包的使用方式详细介绍 1:将拓展包解压:ThinkPHP3.1.2_Extend.zip   --> 将其下的 \ ...

  4. Android消息循环分析

    我们的经常使用的系统中,程序的工作一般是有事件驱动和消息驱动两种方式,在Android系统中,Java应用程序是靠消息驱动来工作的. 消息驱动的原理就是: 1. 有一个消息队列.能够往这个队列中投递消 ...

  5. 新闻专栏~ART让Android更流畅

    潘鹏在CSDN上原创,如其它站点转载请注意排版和写明出处: ART.Android新的内存垃圾回收机制 ART的优点:代码加载速度加快----编译次数降低 Android的卡顿是由于内存垃圾回收机制. ...

  6. OCP读书笔记(15) - 管理SQL性能调优

    SQL Tuning Advisor(STA): 使用oracle提供的程序包进行sql优化 SQL> conn scott/tiger SQL), name )); SQL> inser ...

  7. Javascript新手集中营

        javascript是世界上最流行的编程语言,也许没有之一,看看github,stackoverflow上面的开源项目和问答就可略知一二.它可以用来开发web app.服务器.或者联合nati ...

  8. Linux中利用crontab创建计划任务

    在linux中启动crontab服务: /etc/init.d/crond  start crontab的命令格式 crontab -l   显示当前的crontab 文件(默认编写的crontab文 ...

  9. 就是这么简单(续)!使用 RestAssuredMockMvc 测试 Spring MVC Controllers(转)

    接我前面一篇文章关于RestAssured测试Restful web service的, RestAssured还有一个功能, 使用RestAssuredMockMvc 单元测试你的Spring MV ...

  10. oschina 建站系统

    建站系统 分类网站程序(9) 众筹平台(2) 团购网站系统(14) 开源轻博客系统(8) 开源博客系统(279) 视频网站系统(9) 开源微博工具(93) 论坛系统BBS(129) 建站系统CMS(5 ...