【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: " ...
随机推荐
- dubbo 入门
1 介绍 1.1 背景 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 1.2 说明 DUBB ...
- 如何创建并运行java线程
本文转载地址: http://ifeve.com/creating-and-starting-java-threads/ Java线程类也是一个object类,它的实例都继承自j ...
- 029 hibernate抓取策略
实例A引用实例B,B如果是代理的话(比如多对一关联中):如果遍历A的查询结果集(假设有10条记录),在遍历A的时候,访问B变量,将会导致n次查询语句的发出!这个时候,如果在B一端的class上配置ba ...
- 【Netty】Netty之Bootstrapping
一.前言 前面已经学习了Netty的EventLoop以及线程模型,接着学习Netty的Bootstrapping. 二.Bootstrapping 在学习了Netty中的很多组件后,如何将这些组件有 ...
- python爬虫从入门到放弃(四)之 Requests库的基本使用
什么是Requests Requests是用python语言基于urllib编写的,采用的是Apache2 Licensed开源协议的HTTP库如果你看过上篇文章关于urllib库的使用,你会发现,其 ...
- Javascript创建类和对象
现总结一下Javascript创建类和对象的几种方法: 1.原始的创建方法: <script type="text/javascript"> var person = ...
- 写给Android App开发人员看的Android底层知识(7)
(十二)ContentProvider (1)ContentProvider是什么? ContentProvider,简称CP. 做App开发的同学,尤其是电商类App,对CP并不熟悉,对这个概念的最 ...
- 移动端页面开发适配 rem布局原理
主题 HTML移动端页面开发适配 rem布局原理 什么是适配,为什么要适配 我们拿到的设计图一般是以640,750,1080分辨率为基准设计的,而现在的手机终端各式各样,分辨率不同,逻辑像素不同 ,适 ...
- Azure 基础:Blob Storage
Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob.Queue.File 和 Table. 笔者在前文中介绍了 Table Storage 的基本 ...
- oracle日期时间函数 总结
表中存在伪列:sysdate,systimestamp 伪列存在但是不显示 select sysdate from dual; select systimestamp from dual; 日期计算公 ...