1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public class Solution { public List<String> findRepeatedDnaSequences(String s) { List<String> result = new ArrayList<String>(); HashMap<Integer, Integer> lookup = new HashMap<Integer, Integer>(); String current = ""; int sum = 0; for (int i = 0; i < s.length(); i++){ sum = ((sum << 2)+getMap(s.charAt(i))) & 0xFFFFF; current += s.charAt(i); if (current.length() == 10){ if (lookup.containsKey(sum)){ if (lookup.get(sum) == 1){ result.add(current); lookup.put(sum,2); } }else{ lookup.put(sum,1); } current = current.substring(1,10); } } return result; } int getMap(char a){ if (a == 'A') return 0; if (a == 'C') return 1; if (a == 'G') return 2; return 3; } } |
Thursday, December 3, 2015
[leetcode]Repeated DNA Sequences
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment