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;
	}
时间: 2024-08-24 12:16:45

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

[LeetCode] 187. Repeated DNA Sequences 求重复的DNA序列

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 seq

[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 seq

LeetCode 187. Repeated DNA Sequences 20170706 第三十次作业

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 seq

LeetCode 187. Repeated DNA Sequences(位运算,hash)

题目 题意:判断一个DNA序列中,长度为10的子序列,重复次数超过1次的序列! 题解:用一个map 就能搞定了,但是出于时间效率的优化,我们可以用位运算和数组代替map,首先只有四个字母,就可以用00,01,10,11 四个二进制表示,长度为10的序列,可以用长度为20的二进制序列表示.这样每中组合都对应一个数字,然后用数组表示每个数字出现的次数就好了. class Solution { public: int m[1<<21]; int m3[1<<21]; int m2[127

[leetcode] 187 Repeated DNA Sequences

(一)最一开始的做法是使用 map<string,int> 记录每个10个字符的字符串的个数,超过2就push_back进ans.但是MLE了,说明采用string并不是一个好方法. 下面是MLE的代码: class Solution { public: vector<string> findRepeatedDnaSequences(string s) { vector <string> ans; map<string,int> mp; if(s.lengt

【LeetCode】187. Repeated DNA Sequences

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

187. Repeated DNA Sequences Leetcode Python

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 seq

【LeetCode】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-lon

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