原题地址:

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. Android获取Activity(应用)的执行状态及其它信息

    检測某Activity是否在当前Task的栈顶 public static boolean isTopActivy(String cmdName, Context context) { Activit ...

  2. 《转》div 中间固定 左右自适应实现

    <转自>:http://www.w3cplus.com/css/layout-column-three 对于我来说,这是一种很少碰到的布局方法,不知道大家有何体会,那么下面我们一起来看这种 ...

  3. WPF 设置WebBrowser控件不弹脚本错误提示框

    using System.Reflection; using System.Windows; using System.Windows.Controls; using System.Windows.N ...

  4. C++教材

    C++语言: 1.<Essential C++>:Stanley B.Lipman著. 旁枝暂略,主攻核心,轻薄短小.附习题与解答,适合刚開始学习的人. 2.<The C++ Pro ...

  5. Android.mk编译.apk .so .jar .a第三方.apk .so .jar .a的方法

    一.编译一个简单的APK LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) # Build all java files in the java s ...

  6. jQuery 弹出窗口的形式一直是具体案件的中心

    在网上查 多 不是不符合无效;因此,一些自己总结,解决这个问题   原则: 常见问题: 弹出层居中了,背景也是半透明的  可是发现一拉动滚动栏立即就露馅了发现背景仅仅设置了屏幕所在段,其它部分都是原来 ...

  7. mongodb中的排序和索引快速学习

    在mongodb中,排序和索引其实都是十分容易的,先来小结下排序: 1 先插入些数据    db.SortTest.insert( { name : "Denis", age : ...

  8. 字典实体类:DictionaryEntry类

    DictionaryEntry类是一个字典集合,主要包括的内容是键/值对.这样的组合方式能够方便地定位数据,当中的"键"具备唯一性,类似于数据库中的"id",一 ...

  9. IE浏览器审查密码的清除

    上周去188工业区,总装厂的喷油部的电脑上被谁设置了IE的审查密码,后来通过电话沟通,运维岗给出了具体的解决方法: 进入注册表HKEY_LOCAL_MACHINE\Software\Microsoft ...

  10. Harris角点检测算原理

    主要参考了:http://blog.csdn.net/yudingjun0611/article/details/7991601  Harris角点检测算子 本文将该文拷贝了过来,并做了一些数学方面的 ...