187 Repeated DNA Sequences 重复的DNA序列
所有DNA由一系列缩写为A,C,G和 T 的核苷酸组成,例如:“ACGAATTCCG”。在研究DNA时,识别DNA中的重复序列有时非常有用。
编写一个函数来查找DNA分子中所有出现超多一次的10个字母长的序列(子串)。
详见:https://leetcode.com/problems/repeated-dna-sequences/description/
Java实现:
class Solution {
public List<String> findRepeatedDnaSequences(String s) {
List<String> res = new ArrayList<>();
if(s.length()<10){
return res;
}
Map<String,Integer> m = new HashMap<>();
for(int i=0;i<s.length()-9;i++){
String subString = s.substring(i,i+10);
if(m.containsKey(subString)){
int count=m.get(subString); //如果为1,则添加进结果,否则继续遍历
if(count==1){
res.add(subString);
}
m.put(subString,count+1);
}else{
m.put(subString,1);
}
}
return res;
}
}
C++实现:
class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
vector<string> res;
if (s.size() <= 10)
{
return res;
}
int mask = 0x7ffffff;
unordered_map<int, int> m;
int cur = 0, i = 0;
while (i < 9)
{
cur = (cur << 3) | (s[i++] & 7);
}
while (i < s.size())
{
cur = ((cur & mask) << 3) | (s[i++] & 7);
if (m.find(cur) != m.end())
{
if (m[cur] == 1)
{
res.push_back(s.substr(i - 10, 10));
}
++m[cur];
}
else
{
m[cur] = 1;
}
}
return res;
}
};
参考:https://www.cnblogs.com/grandyang/p/4284205.html
187 Repeated DNA Sequences 重复的DNA序列的更多相关文章
- 187. Repeated DNA Sequences重复的DNA子串序列
[抄题]: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &qu ...
- Leetcode187. Repeated DNA Sequences重复的DNA序列
所有 DNA 由一系列缩写为 A,C,G 和 T 的核苷酸组成,例如:"ACGAATTCCG".在研究 DNA 时,识别 DNA 中的重复序列有时会对研究非常有帮助. 编写一个函数 ...
- [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. 重复的DNA序列(Repeated DNA Sequences)
187. 重复的DNA序列 187. Repeated DNA Sequences 题目描述 All DNA is composed of a series of nucleotides abbrev ...
- [LeetCode] 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 ...
- [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 ...
- [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
题目: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: " ...
随机推荐
- Vs2012在Linux开发中的应用(5):项目属性的定义
VS的项目属性表实际上是由一系列的XML文件定义的,都存放在C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V110\2052文件夹下.我们全然能够 ...
- PHP开发者实用的代码
一.查看邮件是否已被阅读 当你在发送邮件时,你或许很想知道该邮件是否被对方已阅读.这里有段非常有趣的代码片段能够显示对方IP地址记录阅读的实际日期和时间. <? error_reporting( ...
- Python爬虫开发【第1篇】【爬虫案例】
案例一:网站模拟登录 # douban.py from selenium import webdriver from selenium.webdriver.common.keys import Key ...
- 返回零长度的数组或者集合,而不是null
<<Effective Java>> 第四十三条:返回零长度的数组或者集合,而不是null 假设一个方法的返回值类型是集合或者数组 .假设在方法内部须要返回的集合或者数组是零长 ...
- 2016/2/26 <marquee></marquee>实现多种滚动效果
页面的自动滚动效果,可由javascript来实现,但是有一个html标签 - <marquee></marquee>可以实现多种滚动效果,无需js控制.使用marquee标记 ...
- shell 读写远程数据库
http://www.cnblogs.com/wangkangluo1/archive/2012/04/27/2472898.html 利用Shell脚本实现远程MySQL自动查询 目的:对定时任务对 ...
- Android 6.0 RK3288 ROM编译详解+命令详解【转】
本文转载自:http://blog.csdn.net/MLQ8087/article/details/58607692 Android 6.0 RK3288 ROM编译详解+命令详解 原创 2017年 ...
- POJ2594 Treasure Exploratio —— 最小路径覆盖 + 传递闭包
题目链接:https://vjudge.net/problem/POJ-2594 Treasure Exploration Time Limit: 6000MS Memory Limit: 655 ...
- POJ - 2418 Hardwood Species(map,trie,BST)
1.输入若干行树名,输入结束后,按字典序输出树名及其所占百分比. 2.多种方法:map,trie,BST 3. map: #include<iostream> #include<st ...
- HttpWebRequest以及HttpWebResponse
上次介绍了用WebClient的方式提交POST请求,这次,我继续来介绍用其它一种方式 HttpWebRequest以及HttpWebResponse 自认为与上次介绍的WebClient最大的不同之 ...