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"].
链接: http://leetcode.com/problems/repeated-dna-sequences/
题解:
求repeating molecule of DNA sequence。最直接的想法就是从头遍历,把substring加入到HashMap中,然后进行比较。这样的话因为substring()的复杂度是O(n),所以整个算法复杂度是O(n2)。看到有讨论用Rabin-Karp的Rolling Hash自己做Hash Function。这样做的好处我觉得可能是减少了substring()的次数,但总得来说时间复杂度也还是O(n2)。而且做了Rabin-Karp的话要不要要不要判断collision,collision以后用Monte-carlo还是Las Vegas检测结果,也是问题。要再研究一下。
Time Complexity - O(n2), Space Complexity - O(n)。
public class Solution {
public List<String> findRepeatedDnaSequences(String s) {
List<String> res = new ArrayList<String>();
if(s == null || s.length() < 10)
return res;
Map<String, Integer> map = new HashMap<>(); for(int i = 0; i < s.length() - 9; i++) {
String subStr = s.substring(i, i + 10);
if(map.containsKey(subStr)) {
if(map.get(subStr) == 1)
res.add(subStr);
map.put(subStr, map.get(subStr) + 1);
} else
map.put(subStr, 1);
} return res;
}
}
二刷:
还是用的老方法,利用HashMap进行比较。
看到Stefan的用Set也能做,更像Python的风格,速度更快。
Rolling Hash的话,Time Complexity是O(n),但每次都要计算10个数的hash value,速度也不快。有机会的话联系一下好了。
Java:
HashMap:
Time Complexity - O(n2), Space Complexity - O(n)。
public class Solution {
public List<String> findRepeatedDnaSequences(String s) {
List<String> res = new ArrayList<>();
if (s == null) return res;
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i + 10 <= s.length(); i++) {
String str = s.substring(i, i + 10);
if (!map.containsKey(str)) {
map.put(str, 1);
} else {
if (map.get(str) == 1) res.add(str);
map.put(str, 2);
}
}
return res;
}
}
Reference:
https://leetcode.com/discuss/24595/short-java-rolling-hash-solution
https://leetcode.com/discuss/24557/just-7-lines-of-code
https://leetcode.com/discuss/24478/i-did-it-in-10-lines-of-c
https://leetcode.com/discuss/25399/clean-java-solution-hashmap-bits-manipulation
https://leetcode.com/discuss/25536/am-understanding-the-problem-wrongly-what-about-aaaaccccca
https://leetcode.com/discuss/29623/11ms-solution-with-unified-hash-fxn
https://leetcode.com/discuss/54777/easy-to-understand-java-solution-with-well-commented-code
https://leetcode.com/discuss/46948/accepted-java-easy-to-understand-solution
https://leetcode.com/discuss/64841/7-lines-simple-java-o-n
187. Repeated DNA Sequences的更多相关文章
- [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
题目: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: " ...
- 187. Repeated DNA Sequences重复的DNA子串序列
[抄题]: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &qu ...
- 187. Repeated DNA Sequences (String; Bit)
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- 187. Repeated DNA Sequences(建立词典,遍历一遍 o(n))
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
随机推荐
- 在SQL脚本中的注释引起的奇怪问题
在数据库安装包中,我们通过osql.exe这个工具来对相关的数据库脚本进行更新,昨天突然发现安装包报错了,说脚本错误,但我们将脚本拿到数据库查询分析器中执行,一切OK. 问题出在哪里呢? 通过使用os ...
- [CSS]浮动的那点事儿
元素是怎样浮动 元素的水平方向浮动,意味着元素只能左右移动而不能上下移动. 一个浮动元素会尽量向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止. 浮动元素之后的元素将围绕它. 浮动元素 ...
- NSS_02 日志配置
采用log4net,使用系统推荐的最新版本:log4net-1.2.11-bin-newkey.zip(网址:http://logging.apache.org/log4net/download_lo ...
- discuz论坛X3升级时 文件下载出现问题,请查看您的服务器网络以及data目录是否有写权限
discuz论坛2.5升级X3时候, 在线升级一半提示: 文件 static/image/postbg/3.jpg 下载出现问题,请查看您的服务器网络以及data目录是否有写权限,请确认无误后点击确定 ...
- React-router 要点
1.关于url中传参的问题 比如我想打开: /articles/detail/101 在url中要传一个参数 /articles/detail/:articleId 路由中:<Route pat ...
- Clone table header and set as the first element, and replace header's th with td
Clone table header and replace header's th with td var tableHeaderRow = '#tableId tbody tr:nth-child ...
- @Html.TextBox 的使用
@Html.TextBox( }); //限制 text 的最大输入字符数为 10个 @Html.TextBox("users","",new {@class= ...
- 不得不知道的Python字符串编码相关的知识
开发经常会遇到各种字符串编码的问题,例如报错SyntaxError: Non-ASCII character 'ascii' codec can't encode characters in posi ...
- vs2010配备boost编程环境
vs2010配备boost编程环境 vs2010配置boost编程环境 第一步:下载boost,我下载的方法是从http://www.boost.org/上找最新的下载.名字叫boost_1_53_0 ...
- 1010. Radix (25)
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...