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. Chrome商店Crx离线安装包下载

    第一步:找到Chrome的扩展应用ID 第二步:输入扩展应用ID 第三步:单击 生成 按钮. 第四步:在这里右键另存为即可下载.

  2. Yii2结合webuploader实现图片上传

    js中 uploader = WebUploader.create({ // 自动上传. auto : true, // swf文件路径 swf : 'webuploader/Uploader.swf ...

  3. MySQL tips (日期时间操作/concat 等)

    1.  Query结尾要加一个分号: 2.  数据库和表 SHOW DATABASES;    USE YOUR_DB; SHOW TABLES; SHOW COLUMNS FROM study或者D ...

  4. JSP业务逻辑层

    经典的三层架构:表示层.业务逻辑层和数据访问层 具体的区分方法 1:数据访问层:主要看你的数据层里面有没有包含逻辑处理,实际上他的各个函数主要完成各个对数据文件的操作.而不必管其他操作. 2:业务逻辑 ...

  5. groovy-正则表达式

    Groovy使用~”pattern” 来支持正则表达式,它将使用给定的模式字符串创建一个编译好的Java Pattern 对象.Groovy也支持 =~(创建一个Matcher)和 ==~ (返回bo ...

  6. POJ2677 Tour(DP+双调欧几里得旅行商问题)

    Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3929   Accepted: 1761 Description ...

  7. easyUI框架之学习记录汇总

    在添加完之后,可以使用 $.parser.parse();这个方法进行处理:(1) 对整个页面重新渲染: $.parser.parse(); (2) 渲染某个特定的objectvar targetOb ...

  8. ecshop后台"云提醒未激活 点击激活" 补丁删除方法

    ecshop后台"云提醒未激活 点击激活" 补丁删除方法 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2015-01-15   ecshop后台提 ...

  9. LUA的编译、环境等

    Lua的环境.编译等 Lua命令行 lua命令行选项: -i:进入交互式 -e:执行lua代码 -l:加载库文件 例如使用下面的命令启动lua解释器,可以重新定义lua提示符. lua -i -e & ...

  10. MSF溢出实战教程

    1.  进入终端,开启MSF相关服务 2.  连接数据库 3.  主机扫描 发现如果有MS08_067漏洞,就可以继续渗透 4.  开始溢出 溢出成功的话 sessions -l         查看 ...