1 题目

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

接口

public List<String> findRepeatedDnaSequences(String s);

2 思路

寻找重复出现1次以上的10个字符串。

思路1:TLE。从第一个子字符串开始遍历,并存储在List中,如果某个子字符串出现两次,就将其添加到结果List中。

思路2:映射4个字符'A''C''G''T'为整数,对整数进行移位以及位与操作,以获取相应的子字符串。

①将ACGT进行二进制编码

A -> 00

C -> 01

G -> 10

T -> 11

②在编码的情况下,每10位字符的组合是一个数字value,且10位的字符串有20位;int是32位,可以储存。例如

ACGTACGTAC -> 00011011000110110001

AAAAAAAAAA -> 00000000000000000000

采用HashSet来存储这些value。20位的二进制数,至多有2^20种组合,因此hash set的大小最大为2^20,即1024 * 1024。

③遍历字符串

每次向右移动1位字符,相当于字符串对应的int值左移2位,再将其最低2位置为新的字符的编码值,最后将高2位置0。

 value = (value << 2) + 字符的编码值int;
value &= (1 << 20) - 1;//整数占32个位,获取其低20位(题中要求是长度为10的子字符串,映射为整数后,子字符串总共占用20位)

例如

src CAAAAAAAAAC

subStr CAAAAAAAAA

int 0100000000

subStr AAAAAAAAAC

int 0000000001

复杂度

Time:O(n)  Space:O(n) 

3 代码

 import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set; public class Solution {
// Time:O(n) Space:O(n)
private final static Map<Character, Integer> chsMap = new HashMap<Character, Integer>(
4);
static {
chsMap.put('A', 0);
chsMap.put('C', 1);
chsMap.put('G', 2);
chsMap.put('T', 3);
} public List<String> findRepeatedDnaSequences(String s) {
Set<String> res = new HashSet<String>();
final int length = s.length();
if (length <= 10)
return new LinkedList<String>(res);
Set<Integer> intSet = new HashSet<Integer>();
int value = 0;
for (int i = 0; i < 9; i++) {
value = (value << 2) + chsMap.get(s.charAt(i));
}
for (int i = 9; i < length; i++) {
value = (value << 2) + chsMap.get(s.charAt(i));
value &= (1 << 20) - 1;
if (intSet.contains(value)) {
res.add(s.substring(i - 9, i + 1));
}
intSet.add(value);
}
return new LinkedList<String>(res);
}
}

4  扩展

  • 当该字符串中没有出现长度为10的子字符串出现两次以上时,返回结果为[],而不是null。
  • 若输入'AAAAAAAAAAA', 输出['AAAAAAAAAA']

5 参考

  1. leetcode
  2. Leetcode:Repeated DNA Sequences详细题解

lc面试准备:Repeated DNA Sequences的更多相关文章

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

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

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

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

  3. [LeetCode] 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] Repeated DNA Sequences

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

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

  6. 【leetcode】Repeated DNA Sequences(middle)★

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

  7. LeetCode() Repeated DNA Sequences 看的非常的过瘾!

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

  8. Repeated DNA Sequences

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

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

随机推荐

  1. android开发之bitmap使用

    bitmap是android中重要的图像处理工具类,通过bitmap可以对图像进行剪切.旋转.缩放等操作,同时还可以指定格式和压缩质量保存图像文件. 一.拿到一个Bitmap对象 查看源码我们知道,B ...

  2. git 删除配置的远程地址

    删除(origin 名称需根据你本地查询出来的想删除的名字, 查询命令为 git remote -v) git remote rm origin 添加(origin 名称可根据需要添加) git re ...

  3. Hadoop书籍汇总

    <Hadoop实战>陆嘉恒 <Hadoop - The Definitive Guide>Tom White,中文版<Hadoop权威指南> <Hadoop技 ...

  4. 3G/4G网卡使用

    整体架构: pppd call option & ----------↓---------- option脚本(设置PPP连接) ----------↓---------- chat脚本(进行 ...

  5. Crawler & Ajax:WebBrowser C#

    Crawler 與 Ajax http://net.zdnet.com.cn/network_security_zone/2007/1005/536329.shtml WebBrowser: 利用We ...

  6. (转)JS获取当前对象大小以及屏幕分辨率等

    Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> ...

  7. React+Immutable.js的心路历程

    这段时间做的项目开发中用的是React+Redux+ImmutableJs+Es6开发,总结了immutable.js的相关使用姿势: Immutable Data 顾名思义是指一旦被创造后,就不可以 ...

  8. Actioncontext跟ServletActionContext的区别---未完待续

    //public class BaseAction extends ActionSupport{ public static HttpServletRequest getRequest(){ retu ...

  9. 反射+javacsv+scv文件构建资源获取

    1.反射之前已经写过,大家可以在http://www.cnblogs.com/myadmin/p/5282628.html中大概了解下.同时请大家指正. javacsv.jar:java中用来处理cs ...

  10. 在CentOS6.0上安装Oracle 11gR2 (11.2.0.1)以及基本的配置(一)

    首先安装CentOS6.0   就不用说了.安装即可.唯一需要注意的就是后面Oracle 11G Installation guide中的Checking the Software Requireme ...