1. Two Sum
Easy
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution.
Example: Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
UPDATE (2016/2/13):
The return format had been changed to zero-based indices. Please read the above updated description carefully.
Explaination
Brute force:
Using two for loop to find the pair of elements whose sum will be the target, time complexity is O(n^2)
Improve way:
Using a hashmap to store the previous element and its index, and the time complexity is O(1), but the space complexity is O(n).
public class Solution {
// test case:
// array contains 0 element: []
// array contains 1 element: [1]
// array contains 2 elements but not contain the target: [1, 2], target = 5
// array contains duplicate: [1, 2, 2], target = 3
public int[] twoSum(int[] nums, int target) {
if(nums == null || nums.length <= 1) {
return new int[] {-1, -1};
}
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; i++) {
if(map.containsKey(target - nums[i])) {
return new int[] {map.get(target - nums[i]), i};
} else {
map.put(nums[i], i);
}
}
return new int[] {-1, -1};
}