题目:

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的更多相关文章

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

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

  2. 【原创】leetCodeOj --- Repeated DNA Sequences 解题报告

    原题地址: https://oj.leetcode.com/problems/repeated-dna-sequences/ 题目内容: All DNA is composed of a series ...

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

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

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

  6. [LeetCode#187]Repeated DNA Sequences

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

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

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

  8. [leetcode]187. Repeated DNA Sequences寻找DNA中重复出现的子串

    很重要的一道题 题型适合在面试的时候考 位操作和哈希表结合 public List<String> findRepeatedDnaSequences(String s) { /* 寻找出现 ...

  9. 187. Repeated DNA Sequences

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

随机推荐

  1. linux vi 报错 E37: No write since last change (add ! to override)

    用 vi 命令编辑文本文件,没有文件写入权限的时候会报这个错.:q   :wq   怎么都不能退出. 这时只需 ctrl+z 即可,或者在退出命令后加 ! 忽略提示     :q!

  2. Redux-saga

    Redux-saga学习笔记 概述 Redux-saga在Redux应用中扮演'中间件'的角色,主要用来执行数据流中的异步操作.主要通过ES6中的generator函数和yield关键字来以同步的方式 ...

  3. BM算法详解

    http://www-igm.univ-mlv.fr/~lecroq/string/node14.html http://www.cs.utexas.edu/users/moore/publicati ...

  4. JAVA CyclicBarrier类详解

    一个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point).在涉及一组固定大小的线程的程序中,这些线程必须不时地互相等待,此时CyclicBarrie ...

  5. Windows下安装Nodejs步骤

      最近打算把我们的微信端用Vue.js重构,为什么选择Vue.js,一是之前使用的是传统的asp.net mvc,多页面应用用户体验比单页面要差.二是使用过Angular.js,感觉对开发人员要求较 ...

  6. 基本DOS命令之 netstat 命令详解

     netstat 命令(查看端口) netstat 命令用于显示与 IP .TCP .UDP 和 ICMP 协议相关的统计数据,一般用于检验本机各端口的网络连接情况,可以使用 netstat 命令查看 ...

  7. android 本地数据库sqlite的封装

    单机android   sqlite数据库的实现,这个数据库可与程序一起生成在安装包中 一.下载sqlite3.exe文件 二.运行 cmd 转到sqlite3.exe 所在目录  运行 sqlite ...

  8. 基于Redis实现分布式锁(1)

    转自:http://blog.csdn.net/ugg/article/details/41894947 背景在很多互联网产品应用中,有些场景需要加锁处理,比如:秒杀,全局递增ID,楼层生成等等.大部 ...

  9. meta标签属性总结

    一.常见name属性.不同参数含义 meta标签的name属性语法格式是: <meta name="参数" content="具体的参数值"> 其中 ...

  10. 学习css3中的动画

    css animations 主要有两块构成,那么是哪两块呢? keyframes : 定义了什么阶段展示什么样的动画 animation 属性 :把动画挂载到一个具体的dom上,并且定义如何动起来: ...