1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class TwoSum { HashMap<Integer, Integer>lookup = new HashMap<Integer, Integer>(); ArrayList<Integer> numbers = new ArrayList<Integer>(); public void add(int number) { int count = 1; if (lookup.containsKey(number)) count += lookup.get(number); lookup.put(number, count); numbers.add(number); } public boolean find(int value) { for (int i = 0; i < numbers.size(); i++){ int target = value-numbers.get(i); if (target == numbers.get(i)){ if (lookup.containsKey(target) && lookup.get(target) >= 2){ return true; } }else if (lookup.containsKey(target)) return true; } return false; } } |
Wednesday, December 2, 2015
[leetcode]Two Sum III
two sum问题一直ignore的一个细节就是要check target-value[i]是不是map向同一个数字..而这个数字是否只存在一个...
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment