[LeetCode] Repeated DNA Sequences
来源:互联网 发布:淘宝客微博推广教程 编辑:程序博客网 时间:2024/05/23 00:02
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 class Solution { public List<String> findRepeatedDnaSequences(String s) { HashMap<String,Integer> map=new HashMap<String,Integer>(); List<String> re=new ArrayList<String>(); for(int i=0;i<s.length()-9;i++){ String str=s.substring(i,i+10); if(map.containsKey(str)&&map.get(str)==1){ re.add(str); } Integer a=map.get(str); if(a==null) a=0; map.put(s,a+1); } return re; }}
方法二:
上一种方法用的是Hash,如果想要在优化,就是对每个长度为10的substring进行编码,这样连hash都不用,直接通过Index定位即可(因为Hash内部获取String的HashCode,然后通过得到的Hash编码定位,进而与这个key对应得value进行比较),这样最少是需要2*10的比较次数的(当然,不存在和有Hash冲突除外)。而如果可能的种类是有限的(且小于Integer.MAX_VALUE),如本题目,10个字母(4种类型)的排列方式是有限的,由于可以直接用数组,这样不用做2*10,只需要编码和定位就可以了(10)。
public class Solution2 {//上一种方法用的是Hash,如果想要在优化,就是对每个长度为10的substring进行编码,//这样连hash都不用,直接通过Index定位即可(因为Hash内部获取String的HashCode,然后通过得到的Hash编码定位,进而与这个key对应得value进行比较)//这样最少是需要2*10的比较次数的(当然,不存在和有Hash冲突除外)//而如果可能的种类是有限的(且小于Integer.MAX_VALUE),如本题目,10个字母(4种类型)的排列方式是有限的,由于可以直接用数组,这样不用做2*10,只需要编码和定位就可以了(10) public List<String> findRepeatedDnaSequences(String s) {List<String> re=new ArrayList<String>();int[] map=new int[1024*1024];for(int i=0;i<s.length()-9;i++){String str=s.substring(i,i+10);Integer key=encode(str);if(map[key]++==1){re.add(str);}}return re; } public int encode(String s){int re=0;for(int i=0;i<10;i++){re<<=2;//4种类型 用两位表示即可switch(s.charAt(i)){case 'A': re+=0;break;case 'C': re+=1;break;case 'T': re+=2;break;case 'G': re+=3;break;}}return re;}}
阅读全文
1 0
- Leetcode Repeated DNA Sequences
- Repeated DNA Sequences [leetcode]
- [LeetCode] Repeated DNA Sequences
- Leetcode Repeated DNA Sequences
- Leetcode:Repeated DNA Sequences
- Leetcode: Repeated DNA Sequences
- LeetCode: Repeated DNA Sequences
- LeetCode: Repeated DNA Sequences
- LeetCode Repeated DNA Sequences
- LeetCode--Repeated DNA Sequences
- [LeetCode]Repeated DNA Sequences
- [Leetcode]Repeated DNA Sequences
- [leetcode]Repeated DNA Sequences
- Repeated DNA Sequences - LeetCode
- Leetcode: Repeated DNA Sequences
- Leetcode:Repeated DNA Sequences
- leetcode:Repeated DNA Sequences
- LeetCode - Repeated DNA Sequences
- Java Web基础之文件下载
- JSON basics
- Git使用:代码提交、同步,创建分支等
- POJ 2033 Alphacode 笔记
- Git之工作区和暂存区
- [LeetCode] Repeated DNA Sequences
- Java 8 函数式编程
- java的跨平台原理
- Compare Version Numbers字符串的应用+小技巧
- 计算日期到天数转换
- 栈的构建
- 剑指Offer第36题—Java版
- java.util.HashMap源码解析
- C#——面向对象——重载操作符——自定义转换