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"].
解题思路一:

直接用HashMap实现,JAVA实现如下:

    static public List<String> findRepeatedDnaSequences(String s) {
List<String> list=new ArrayList<String>();
HashMap<String,Integer> hm=new HashMap<String,Integer>();
for(int i=0;i<=s.length()-10;i++){
if(hm.containsKey(s.substring(i,i+10)))
list.add(s.substring(i,i+10));
else hm.put(s.substring(i,i+10), 1);
}
return list;
}

结果Memory Limit Exceeded

解题思路二:

模拟Hash,将A、C、G、T分别变为0、1、2、3,然后每10位计算下hashcode,如果hashcode所在的count为1则输出,JAVA实现如下:

	static int getValue(char ch) {
if (ch == 'A')
return 0;
else if (ch == 'C')
return 1;
else if (ch == 'G')
return 2;
else
return 3;
}
static public List<String> findRepeatedDnaSequences(String s) {
List<String> list = new ArrayList<String>();
if (s.length() <= 10)
return list;
int[] count = new int[(1 << 20)-1];
int hash = 0;
for (int i = 0; i < 9; i++)
hash = (hash << 2) | getValue(s.charAt(i));
for (int i = 9; i < s.length(); i++) {
hash = (1<<20)-1&((hash << 2) | getValue(s.charAt(i)));
if (count[hash]==1)
list.add(s.substring(i - 9, i + 1));
count[hash]++;
}
return list;
}

Java for LeetCode 187 Repeated DNA Sequences的更多相关文章

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

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

  3. [LeetCode#187]Repeated DNA Sequences

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

  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. [leetcode]187. Repeated DNA Sequences寻找DNA中重复出现的子串

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

  6. 【LeetCode】187. Repeated DNA Sequences 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/repeated ...

  7. 【LeetCode】187. Repeated DNA Sequences

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

  8. 187. Repeated DNA Sequences

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

  9. 【LeetCode】Repeated DNA Sequences 解题报告

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

随机推荐

  1. Java基础-数据类型int,short,char,long,float,double,boolean,byte

    Java语言是静态类型的(statical typed),也就是说所有变量和表达式的类型再编译时就已经完全确定.由于是statical typed,导致Java语言也是强类型(Strong typed ...

  2. bzoj 1193 贪心

    如果两点的曼哈顿距离在一定范围内时我们直接暴力搜索就可以得到答案,那么开始贪心的跳,判断两点横纵坐标的差值,差值大的方向条2,小的条1,不断做,直到曼哈顿距离较小时可以暴力求解. 备注:开始想的是确定 ...

  3. Vijos1459 车展 (treap)

    描述 遥控车是在是太漂亮了,韵韵的好朋友都想来参观,所以游乐园决定举办m次车展.车库里共有n辆车,从左到右依次编号为1,2,…,n,每辆车都有一个展台.刚开始每个展台都有一个唯一的高度h[i].主管已 ...

  4. cannot get gid for group ‘nobody’

    启动php-fpm /data1/server/php-cgi/sbin/php-fpm 错误[28-Mar-2012 11:15:01] ERROR: failed to open configur ...

  5. centos7中systemctl命令使用方法和心得体会

    使用linux的同学对service和chkconfig两个命令都不陌生,其重要性不言而喻,那么怎么会突然冒出个systemctl命令呢?其实,为了简化操作,systemctl命令将service和c ...

  6. Linux samba配置

    更详细的配置地址:http://www.cnblogs.com/mchina/archive/2012/12/18/2816717.html 1 安装  yum -y install samba 2 ...

  7. ssh事务配置

    <!-- 配置业务层 --> <bean id="employeeService" class="cn.bdqn.jboa.service.impl.E ...

  8. 32和64位的CentOS 6.0下 安装 Mono 2.10.8 和Jexus 5.0

    http://www.cnblogs.com/shanyou/archive/2012/01/07/2315982.html shanyou 博客

  9. Entity Framework 自动生成CodeFirst代码

    前言 在前面的文章中我们提到Entity Framework的“Code First”模式也同样可以基于现有数据库进行开发.今天就让我们一起看一下使用Entity Framework Power To ...

  10. Win10如何隐藏Windows Defender任务栏图标

    导读 Windows 10 至发布以来就内置集成了 Windows Defender 安全防护应用,但有许多用户平常压根儿就没注意到它的存在.微软为了使安全防护功能更加明显,Windows 10 周年 ...