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"].

其实就是一个字符串,然后以10个为单位,求重复两次以上的字符串。

1、用一个set就可以实现了。

public class Solution {
public List<String> findRepeatedDnaSequences(String s) {
List<String> list = new ArrayList();
int len = s.length();
if (len <= 10){
return list;
}
HashSet<String> set = new HashSet();
for (int i = 10; i <= len; i++){
String str = s.substring(i - 10, i);
if (set.contains(str) && !list.contains(str)){
list.add(str);
} else {
set.add(str);
}
}
return list;
}
}

2、discuss里面是有一些利用位操作的,例如。

public List<String> findRepeatedDnaSequences(String s) {
Set<Integer> words = new HashSet<>();
Set<Integer> doubleWords = new HashSet<>();
List<String> rv = new ArrayList<>();
char[] map = new char[26];
//map['A' - 'A'] = 0;
map['C' - 'A'] = 1;
map['G' - 'A'] = 2;
map['T' - 'A'] = 3; for(int i = 0; i < s.length() - 9; i++) {
int v = 0;
for(int j = i; j < i + 10; j++) {
v <<= 2;
v |= map[s.charAt(j) - 'A'];
}
if(!words.add(v) && doubleWords.add(v)) {
rv.add(s.substring(i, i + 10));
}
}
return rv;
}

leetcode 187. Repeated DNA Sequences 求重复的DNA串 ---------- java的更多相关文章

  1. [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 ...

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

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

  3. [leetcode]187. Repeated DNA Sequences寻找DNA中重复出现的子串

    很重要的一道题 题型适合在面试的时候考 位操作和哈希表结合 public List<String> findRepeatedDnaSequences(String s) { /* 寻找出现 ...

  4. [LeetCode] 187. Repeated DNA Sequences 解题思路

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

  5. 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 ...

  6. [LeetCode#187]Repeated DNA Sequences

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

  7. LeetCode 187. 重复的DNA序列(Repeated DNA Sequences)

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

  8. 【Leetcode】【Medium】Repeated DNA Sequences

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

  9. Java实现 LeetCode 187 重复的DNA序列

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

随机推荐

  1. low-rank 的相关求解方法 (CODE) Low-Rank Matrix Recovery and Completion via Convex Optimization

    (CODE) Low-Rank Matrix Recovery and Completion via Convex Optimization 这个是来自http://blog.sina.com.cn/ ...

  2. tcp的简单介绍

    为什么会有TCP/IP协议 在 世界上各地,各种各样的电脑运行着各自不同的操作系统为大家服务,这些电脑在表达同一种信息的时候所使用的方法是千差万别.就好像圣经中上帝打乱了各地人 的口音,让他们无法合作 ...

  3. Qt之Qwt学习之安装

    QWT+qtcreator 编译.安装使用 目录:一.Qwt简介 二.QWT编译 一.Qwt简介 QWT:Qt Widgets for Technical Applications,是开源的2D绘图库 ...

  4. 设置table距离顶部位置

    //UIEdgeInsetsMake———— UIEdgeInsets insets = {top, left, bottom, right} self.tableView.contentInset ...

  5. impress.js webslide 参数

    data-transition-duration="2000" 改变切换场景的速度,默认1000data-perspective="500" 改变透视的深度,默 ...

  6. jQuery是什么?

    jQuery就是javascript的一个库,把我们常用的一些功能进行了封装,方便我们来调用,提高我们的开发效率. 极大地简化了 JavaScript 编程. Javascipt跟jQuery的区别: ...

  7. jqGrid属性中文详细说明 (转)

    jqGrid的属性很多,其实很大部分的属性,使用其默认值就可以了.但是详细了解一下属性的含义以及作用,对我们定制自己的grid是有帮助的. 以下内容描述格式是:属性名称 参数值类型    描述内容(可 ...

  8. Promise与Defer认识

    1.deffer对象:jquery的回掉函数解决方案:含义是延迟到未来某个点再执行: 2.$.ajax链式写法: $.ajax("test.php")     .done(func ...

  9. C#生成图片缩略图(2种思路)

    前言:在日常图片浏览中,如果图片过多,只有一张张的打开图片才能知道图片的内容,显然这样浏览起来非常不便.Windows系统在浏览图片时提供了缩略图的功能,这样大大的方便了浏览者了解每张图片的内容,本实 ...

  10. JMeter 服务器性能监测插件介绍

    简介 压力测试过程中,能够随时对负载服务器的健康状况的把控是相当重要的,有了这些数据,我们才能准确分析出服务器负载瓶颈.当你面对的是一个集群的时候,如果能了解到负载是否被正确分发,是不是一件很棒的事情 ...