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: "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的更多相关文章
- 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 求重复的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
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寻找DNA中重复出现的子串
很重要的一道题 题型适合在面试的时候考 位操作和哈希表结合 public List<String> findRepeatedDnaSequences(String s) { /* 寻找出现 ...
- 【LeetCode】187. Repeated DNA Sequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/repeated ...
- 【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
题目: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: " ...
- 【LeetCode】Repeated DNA Sequences 解题报告
[题目] All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &quo ...
随机推荐
- 利用getHibernateTemplate实现简单的操作
package org.tarena.dao; import java.sql.SQLException; import java.util.List; import javax.annotation ...
- UTL_FILE详解
包UTL_FILE 提供了在操作系统层面上对文件系统中文件的读写功能.非超级用户在使用包UTL_FILE中任何函数或存储过程前必须由超级用户授予在这个包上的EXECUTE权限.例如:我们使用下列命令对 ...
- 38.Android之ListView简单学习(一)
android中ListView用的很普遍,今天来学习下,本篇主要以本地数据加载到listview,后面会学习从网络获取数据添加到listview. 首先改下布局文件: <?xml versio ...
- 彻底理解position与anchorPoint
引言 相信初接触到CALayer的人都会遇到以下几个问题: 为什么修改anchorPoint会移动layer的位置?CALayer的position点是哪一点呢?anchorPoint与positio ...
- soa vs cop
soa强调分层:底层为高层提供服务: cop强调分块:有明确的职责和服务提供接口,为外部提供服务. SOA 原则非常强调将服务使用者和服务提供者分离开来,关于此类分离实际的含义,有很多不正式但非常有用 ...
- linux4
linux 特点:1.免费 开源(代码公开)2.支持多线程/多用户的操作系统3.安全性4.对内存和文件管理有自己的一套优越的方法 linux最小只需要4M ->嵌入式开发默认不启动用户界面roo ...
- eclipse 左边目录结构下五referenced library解决办法
没有referenced library,加入多个jar包,导致项目看上去庞大 解决办法: 在Package Explorer窗口中会出现Referenced Libraries,但Java EE 透 ...
- xcrun: error: active developer path ("/Volumes/Xcode/Xcode-beta.app/Contents/Developer") does not exist, use `xcode-select --swi
xcrun: error: active developer path ("/Volumes/Xcode/Xcode-beta.app/Contents/Developer") d ...
- IIS负载均衡-Application Request Route详解第一篇: ARR介绍(转载)
IIS负载均衡-Application Request Route详解第一篇: ARR介绍 说到负载均衡,相信大家已经不再陌生了,本系列主要介绍在IIS中可以采用的负载均衡的软件:微软的Applica ...
- 详细解析Java中抽象类和接口的区别
在Java语言中, abstract class 和interface 是支持抽象类定 义的两种机制.正是由于这两种机制的存在,才赋予了Java强大的 面向对象能力.abstract class和in ...