1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class Solution { public int[] twoSum(int[] nums, int target) { int result[] = new int[2]; HashMap<Integer, Integer> lookup = new HashMap<Integer, Integer>(); for (int i = 0; i < nums.length; i++) lookup.put(nums[i],i); for (int i = 0; i < nums.length; i++){ if (lookup.containsKey(target-nums[i]) && lookup.get(target-nums[i])!=i){ result[0] = i+1; result[1] = lookup.get(target-nums[i])+1; break; } } return result; } } |
No comments:
Post a Comment