[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"].
问题:给定一个字符串序列,代表 DNA 序列,求其中有重复出现的长度为 10 的子序列。
题目中的例子都是不重叠的重复字串,实际上相互重叠的字串也是要统计进去,例如11位的 "AAAAAAAAAA" 就包含两个长度为 10 的"AAAAAAAAAA" 的重复子序列。这一点是题目没有说清楚的。
明确题目后,实现思路也比较简单:
- 将 s 中所有长度为 10 的连续子字符串放入 map<string, int> ss_cnt 中,数各个连续字符串出现的的次数
- 将 [0, 9] 视为窗口,将 ss_cnt 中窗口字符串对于的 value 减 1 ,然后判断 ss_cnt 中是否还存在一个 窗口字符串, 若存在则表示窗口字符串是重复的。
- 将窗口向右移动一个,继续重复第二步,直至窗口移至最右端
/**
* 重复子字符串 可以重叠。
*/
vector<string> findRepeatedDnaSequences(string s) {
unordered_set<string> res; unordered_map<string, int> ss_cnt; int len = ; for (int i = ; i + len - < s.size(); i++) {
string str = s.substr(i, len);
ss_cnt[str]++;
} int i = ;
while (i + len - < s.size()) { string cur = s.substr(i, len);
ss_cnt[cur]--; if (ss_cnt[cur] > ) {
res.insert(cur);
} ss_cnt[cur]++;
i++;
} vector<string> result; unordered_set<string>::iterator s_iter;
for (s_iter = res.begin(); s_iter != res.end(); s_iter++) {
result.push_back(*s_iter);
} return result;
}
[LeetCode] 187. Repeated DNA Sequences 解题思路的更多相关文章
- 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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/repeated ...
- [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】Repeated DNA Sequences 解题报告
[题目] All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &quo ...
- 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 ...
- [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寻找DNA中重复出现的子串
很重要的一道题 题型适合在面试的时候考 位操作和哈希表结合 public List<String> findRepeatedDnaSequences(String s) { /* 寻找出现 ...
- 【LeetCode】187. Repeated DNA Sequences
题目: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: " ...
- 【leetcode】Repeated DNA Sequences(middle)★
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
随机推荐
- php-fpm 启动参数及重要配置详解<转>
原文地址 http://levi.cg.am/archives/3127 约定几个目录 /usr/local/php/sbin/php-fpm /usr/local/php/etc/php-fpm. ...
- C#解leetcode 16. 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- dir()函数:罗列出参数所有的功能列表
#coding=utf-8import sysprint dir(sys)#罗列出参数中所有的功能列表sys.__doc__#调用参数中的函数 #dir()函数扩展展详解python中dir()函数不 ...
- jQuery的选择器中的通配符[id^='code']或[name^='code']
这两天在做一个专题的时候遇到了一个通配符的问题 //弹层操作$(function(){ //视频播放 $("a[href^='#video']").each(function(in ...
- asp.net操作cookie类
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary ...
- 【转】 iOS 原生二维码扫描(可限制扫描区域)
在用 AVFoundation 完成扫码后,遇到2个问题: 1,如何限制扫描范围? 2.条形码如何扫描? 一位朋友的文章帮助了我,特地转来,可以帮到有需要的朋友. 原文:http://blog.csd ...
- 03C#基础(2)
1.比较运算符 ==等于; !=不等于; >大于; >=大于或者等于; <小于; <=小于或者等于; 比较运算符(又称关系运算符)用来进行值得真假性判断,结果是boo ...
- PLSQL Package dubug方法
初步接触EBS代码修改,花了几个小时搞明白了Package的debug方法, 1.打开需要测试的package,找到需要测试的过程,右键选择测试 2.在测试窗口中初始化过程的入参,点击测试按钮开始调试 ...
- python密码处理(可用于生产模式)
import os from hashlib import sha256 from hmac import HMAC def encrypt_password(password, salt=None) ...
- 自己总结python用xlrd\xlwt读写excel
1.首先安装xlrd\xlwt模块 xlrd模块下载地址: https://pypi.python.org/pypi/xlrd xlwt模块下载地址: https://pypi.python.org/ ...