Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
Analysis:
See: http://yucoding.blogspot.com/2013/03/leetcode-question-113-two-sum.html
What happens when a duplicate key is put into a HashMap?
You may find your answer in the javadoc of Map#put(K, V) (which actually returns something):
public V put(K key, V value)
Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for this key, the old value is replaced by the specified value. (A map
m
is said to contain a mapping for a keyk
if and only ifm.containsKey(k)
would returntrue
.)Parameters:
key
- key with which the specified value is to be associated.
value
- value to be associated with the specified key.Returns:
previous value associated with specified key, ornull
if there was no mapping forkey
. (Anull
return can also indicate that the map previously associatednull
with the specifiedkey
, if the implementation supportsnull
values.)
So if you don't assign the returned value when calling mymap.put("1", "a string")
, it just becomes unreferenced and thus eligible for garbage collection.
http://stackoverflow.com/questions/1669885/what-happens-when-a-duplicate-key-is-put-into-a-hashmap
Code:
public class Solution {
public int[] twoSum(int[] numbers, int target) {
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int first=0, second=0;
int[] res = new int[2];
for(int i=0; i<=numbers.length-1; i++) {
map.put(numbers[i], i);
}
for(int j=0; j<=numbers.length-1; j++) {
if(map.containsKey(target-numbers[j]) && map.get(target-numbers[j])!=j) { // still correct if duplicates exist !!!
if(map.get(target-numbers[j]) > j) {
res[0] = j + 1;
res[1] = map.get(target-numbers[j]) + 1;
}
else {
res[0] = map.get(target-numbers[j]) + 1;
res[1] = j + 1;
}
break;
}
}
return res;
}
}
Update: 30/01/2014
Use two pointers. However, as for this problem, since the indexes need to be returned, the first approach is much better.
public class Solution {
public int[] twoSum(int[] numbers, int target) {
int[] original = Arrays.copyOf(numbers, numbers.length);
Arrays.sort(numbers);
int low=0, high=numbers.length-1;
int[] res = new int[2];
while(low < high) {
if(numbers[low]+numbers[high]<target) low++;
else if(numbers[low]+numbers[high]>target) high--;
else if(numbers[low]+numbers[high]==target) {
boolean first = false;
for(int i=0; i<original.length; i++) {
if(!first && original[i]==numbers[low]) {
res[0] = i+1;
first = true;
}
if(original[i]==numbers[high]) res[1]=i+1;
}
Arrays.sort(res);
break;
}
}
return res;
}
}
Update 02/08/2014:
public class Solution {
public int[] twoSum(int[] numbers, int target) {
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int[] res = new int[2];
for(int i=0; i<=numbers.length-1; i++) {
if(map.containsKey(target-numbers[i])) { // find the pair
res[0] = map.get(target-numbers[i]);
res[1] = i+1; // the current index must be the larger one
break;
}
else map.put(numbers[i], i+1); // does not match, just store the number and index
}
return res;
}
}