代码1
Runtime: 43 ms, faster than 11.05% of Java online submissions for Repeated DNA Sequences.
class Solution {
public List<String> findRepeatedDnaSequences(String s) {
Set<Integer> first = new HashSet();
Set<Integer> second = new HashSet();
List<String> ans = new ArrayList();
char[] map = new char[26];
map['A' - 'A'] = 0;
map['C' - 'A'] = 1;
map['G' - 'A'] = 2;
map['T' - 'A'] = 3;
for (int i = 0; i + 9 < s.length(); i++) {
String sub = s.substring(i, i + 10);
int sequence = 0;
for (int j = 0; j < 10; j++) {
sequence = sequence << 2;
sequence = sequence | map[sub.charAt(j) - 'A'];
}
if (!first.add(sequence) && second.add(sequence)) ans.add(sub);
}
return ans;
}
}