Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit831e0a6

Browse files
author
applewjg
committed
Repeated DNA Sequences
Change-Id: I9416d5636ca7b038c9d632b1d10b25beb892779e
1 parentc013ad3 commit831e0a6

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

‎RepeatedDNASequences.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Author: Andy, nkuwjg@gmail.com
3+
Date: Feb 3, 2015
4+
Problem: Repeated DNA Sequences
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/repeated-dna-sequences/
7+
Notes:
8+
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.
9+
10+
Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule.
11+
12+
For example,
13+
14+
Given s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT",
15+
16+
Return:
17+
["AAAAACCCCC", "CCCCCAAAAA"].
18+
19+
Solution: ...
20+
*/
21+
22+
publicclassSolution {
23+
publicList<String>findRepeatedDnaSequences(Strings) {
24+
HashMap<Integer,Integer>map =newHashMap<Integer,Integer>();
25+
HashMap<Character,Integer>mole =newHashMap<Character,Integer>();
26+
mole.put('A',0);mole.put('C',1);mole.put('G',2);mole.put('T',3);
27+
List<String>res =newArrayList<String>();
28+
if (s.length() <11)returnres;
29+
intx =0,i =0,mask = (1<<20) -1;
30+
for (;i <10; ++i) {
31+
x = (x <<2) |mole.get(s.charAt(i));
32+
}
33+
map.put(x,1);
34+
for (;i <s.length(); ++i) {
35+
x = (x<<2)|mole.get(s.charAt(i));
36+
x =x &mask;
37+
if (map.containsKey(x)) {
38+
if (map.get(x) ==1) {
39+
res.add(convert2Str(x));
40+
map.put(x,-1);
41+
}
42+
}else {
43+
map.put(x,1);
44+
}
45+
}
46+
returnres;
47+
}
48+
publicStringconvert2Str(intx) {
49+
Stringres =newString();
50+
for (inti =0;i <10; ++i) {
51+
intk =x &3;
52+
if (k ==0)res ='A' +res;
53+
if (k ==1)res ='C' +res;
54+
if (k ==2)res ='G' +res;
55+
if (k ==3)res ='T' +res;
56+
x =x >>2;
57+
}
58+
returnres;
59+
}
60+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp