所有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. jason数据格式 -- 扫盲

    JSON是 JavaScript Object Notation的简称,是一种轻量的数据表示方法.jason格式採用key:value的方式记录数据,非常直观,比XML简洁,因而大受欢迎 介绍jaso ...

  2. coco2dx新建项目报错,ld: -pie can only be used when targeting iOS 4.2 or later clang: error: linker command

    在新建cocos2d-x以后,执行发现下面错误: ld: -pie can only be used when targeting iOS 4.2 or later clang: error: lin ...

  3. Javascript 运行机制

    先看一下下面这段js代码: console.log('1'); setTimeout(function(){ console.log('2'); },0); console.log('3'); 请问打 ...

  4. 抓包工具Fiddler使用宝典之捕获手机报文

    Fiddler 是通过代理来实现数据捕获的.对 Android 手机来说,也是通过将网络连接的代理指向 PC 机的 Fiddler port.来实现数据包的拦截. 以下,我以我的一次实践为例,向大家介 ...

  5. PartialView中的页面重定向

    在MVC的每个action中,都可以指定一种返回页面的类型,可以是ActionResult,这表示返回的页面为view或者是一个PartialView,前台是一个全整页面,后台是页面的一部分. 在以A ...

  6. C#中,变量前的@符号

    看别人写的C#代码,发现有变量前带@,啥意思? string @namespace = "EnterpriseServerBase.WebService.DynamicWebCalling& ...

  7. SpringMVC_基本配置 --跟海涛学SpringMVC(和自己在项目中的实际使用的对比)

    ☆依赖jar包: 1.Spring框架jar 包: 为了简单,将spring-framework-3.1.1.RELEASE-with-docs.zip/dist/下的所有jar包拷贝到项目的WEB- ...

  8. c/c++内存使用原则

    1 no malloc no free 2 no new no delete 如果对象不是new出来的,那么这个对象在生命周期结束后会自动调用析构函数自己释放自己的内存,不需要delete. 但是如果 ...

  9. mysql数据库ip与字符串

    Mysql自带的IP转换语句 inet_aton:将ip地址转换成数字型 inet_ntoa:将数字型转换成ip地址 //使用inet_aton函数,将字符串IP转换为整型: mysql> se ...

  10. jquery cloudzoom 3.0,magiczoom 放大镜插件 破解 移除版权信息

    jquery Cloud Zoom一款放大镜插件.但是无奈 官方下载的始终有版权信息,因此想到如下方法去掉版权信息,测试可行! 官方网址:http://www.starplugins.com/clou ...