所有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序列的更多相关文章

  1. 187. Repeated DNA Sequences重复的DNA子串序列

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

  2. Leetcode187. Repeated DNA Sequences重复的DNA序列

    所有 DNA 由一系列缩写为 A,C,G 和 T 的核苷酸组成,例如:"ACGAATTCCG".在研究 DNA 时,识别 DNA 中的重复序列有时会对研究非常有帮助. 编写一个函数 ...

  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. 重复的DNA序列(Repeated DNA Sequences)

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

  5. [LeetCode] Repeated DNA Sequences 求重复的DNA序列

    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 求重复的DNA串 ---------- java

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

  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. [LeetCode] 187. Repeated DNA Sequences 解题思路

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

  9. 【LeetCode】187. Repeated DNA Sequences

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

随机推荐

  1. Axure使用笔记

    软件设置类 两个矩形的双边框,边框重合: 项目---项目设置---边界对齐---内边界对齐. 自动备份时间设置 文件-自动备份设置-默认15分钟,根据电脑硬件可以调整. Axure 8 可以不用安装 ...

  2. 【小技能】如何检索苹果APP

    有时候要临时在PC上查询一下苹果APP的信息,但是又没有安装itunes软件,那么你可以在必应里面使用类似如下的语句,例如来搜索“aboboo site:itunes.apple.com”,就可以搜索 ...

  3. Intellig Idea2017新建Web项目(tu'wen)

    1.新建新工程项目 2.选择Java 和JDK版本 3.下一步Next(默认不勾选) 4.设置Project Name ,点击More Setting图标可以折叠.展开  然后Finish 我们可以看 ...

  4. CRM 2011 开发中遇到的问题小结

    1.将Retrive 方法改成 RetrieveMultiple时 如果指定的ColumnSet 没有指定主键(entiryname+id),要显示增加实体的主键.否则在调用 Retrieve方法时返 ...

  5. USACO26 moofest 奶牛集会(归并排序)

    听说这题也是bzoj的3378&&poj1990,然而没有权限号交不了..poj懒得登. 题意:有n个奶牛,他们相互发出max(a[i].v,a[j].v)*abs(a[i].p-a[ ...

  6. Module in powershell

    https://docs.microsoft.com/en-us/powershell/module/powershellget/install-module?view=powershell-6 ht ...

  7. BZOJ_4459_[Jsoi2013]丢番图_数学+分解质因数

    BZOJ_4459_[Jsoi2013]丢番图_数学+分解质因数 Description 丢番图是亚历山大时期埃及著名的数学家.他是最早研究整数系数不定方程的数学家之一. 为了纪念他,这些方程一般被称 ...

  8. 【转】Commonjs规范及Node模块实现

    前言: Node在实现中并非完全按照CommonJS规范实现,而是对模块规范进行了一定的取舍,同时也增加了少许自身需要的特性.本文将详细介绍NodeJS的模块实现 引入 nodejs是区别于javas ...

  9. Python 函数定义以及参数传递

    Python 函数定义以及参数传递1.函数定义#形如def func(args...): doSomething123以关键字def 开头,后面是函数名和参数下面是函数处理过程. 举例: def ad ...

  10. RDA GUI

    创建服务:“sysapp_table.h” / "dvb_guiobj_table.h" / "atv_guiobj_table.h" “服务”与“回调”   ...