[LeetCode] 187. Repeated DNA Sequences Java

题目:

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"].

 题意及分析:给出一个字符串,其中只有A,C,G,T四个字母,每10个字母作为一个子字符串,要求找到出现不止一次的子字符串。这道题直接用hastable方法求解,遍历字符串,对每个子字符串做判断,若在hashtable中不存在,就添加进去;若存在,如果出现的次数为1,那么将其添加进结果中,并更新出现次数,否则继续遍历。还有一种方法是将a,c,g,t使用3位bit来保存,然后10个字母,就30bit,这样就可以用一个整数来保存。

代码:

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
public class Solution {
    public List findRepeatedDnaSequences(String s) {
        List res = new ArrayList<>();
        Hashtable temp = new Hashtable<>();

        for(int i=0;i

 

  

 

 

 

 

 

转载于:https://www.cnblogs.com/271934Liao/p/7159613.html

你可能感兴趣的:([LeetCode] 187. Repeated DNA Sequences Java)