Two Sum
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
[code]
public class Solution {
public int[] twoSum(int[] numbers, int target) {
int[] r= new int[2];
if( numbers==null || numbers.length==0)return r;
HashMap<Integer, ArrayList<Integer>> map=new HashMap<Integer, ArrayList<Integer>>();
ArrayList<Integer> l=new ArrayList<Integer>();
l.add(0);
map.put(numbers[0], l);
for(int i=1;i<numbers.length;i++)
{
int val=target-numbers[i];
if(map.containsKey(val))
{
r[0]=map.get(val).get(0)+1;r[1]=i+1;return r;
}
if(map.containsKey(numbers[i]))
{
map.get(numbers[i]).add(i);
}
else
{
ArrayList<Integer> list=new ArrayList<Integer>();
list.add(i);
map.put(numbers[i], list);
}
}
return r;
}
}
[Thoughts]
排序+2 pointers 需要O(nlgn) time, but no extra space;
HashMap 需要O(n) time, but O(n) space.
HashMap <xxx, ArrayList<xxx>>
可以解决duplicate key的问题
3 Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note:
Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
The solution set must not contain duplicate triplets.
For example, given array S = {-1 0 1 2 -1 -4},
A solution set is:
(-1, 0, 1)
(-1, -1, 2)
[code]
public class Solution {
public List<List<Integer>> threeSum(int[] num) {
List<List<Integer>> list=new ArrayList<List<Integer>>();
if(num==null || num.length<3 )return list;
Arrays.sort(num);
int i=0, j ,k;
while(i<num.length-2)
{
j=i+1;k=num.length-1;
int val=0-num[i];
while(j<k)
{
if(num[j]+num[k]<val)j++;
else if(num[j]+num[k]>val)k--;
else
{
ArrayList<Integer> l=new ArrayList<Integer>();
l.add(num[i]);l.add(num[j]);l.add(num[k]);
list.add(l);
j++;k--;
while(j<num.length && num[j]==num[j-1])j++;
while(k>j && num[k]==num[k+1])k--;
}
}
i++;
while(i<num.length-2 && num[i]==num[i-1])i++;
}
return list;
}
}
[Thoughts]
排序+2pointers, O(n^2)
3 sum closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
For example, given array S = {-1 2 1 -4}, and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
[code]
public class Solution {
public int threeSumClosest(int[] num, int target) {
if(num==null || num.length<3)return 0;
Arrays.sort(num);
int i, j, k, closet=0, localMin=Integer.MAX_VALUE;
for(i=0;i<num.length-2;i++)
{
j=i+1;k=num.length-1;
while(j<k)
{
int sum=num[i]+num[j]+num[k];
if(sum==target)return target;
else if(sum>target)
{
if(sum-target<localMin)
{
localMin=sum-target;closet=sum;
}
k--;
}
else
{
if(target-sum<localMin)
{
localMin=target-sum;closet=sum;
}
j++;
}
}
}
return closet;
}
}
[Thoughts]
4Sum (nSum的递归实现)
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note:
Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
The solution set must not contain duplicate quadruplets.
For example, given array S = {1 0 -1 0 -2 2}, and target = 0.
A solution set is:
(-1, 0, 0, 1)
(-2, -1, 1, 2)
(-2, 0, 0, 2)
[code]
public class Solution {
public List<List<Integer>> fourSum(int[] num, int target) {
if(num==null || num.length<4)return new ArrayList<List<Integer>>();
Arrays.sort(num);
return nSum(num, 0, 4, target);
}
public List<List<Integer>> nSum(int[] num, int start, int n, int target)
{
List<List<Integer>> list=new ArrayList<List<Integer>>();
if(n==1)
{
int low=start, high=num.length-1, mid=(low+high)/2;
boolean onesum=false;
while(low<=high)
{
mid=(low+high)/2;
if(num[mid]==target)
{
onesum=true;
break;
}
else if(num[mid]<target)low=mid+1;
else high=mid-1;
}
if(onesum)
{
ArrayList<Integer> temp=new ArrayList<Integer>();
temp.add(target);
list.add(temp);
}
return list;
}
int i=start;
while(i<=num.length-n)
{
int val=target-num[i];
List<List<Integer>> l=nSum(num, i+1, n-1, val);
for(List<Integer> li:l)
{
li.add(0,num[i]);
list.add(new ArrayList<Integer>(li));
}
i++;
while(i<=num.length-n && num[i]==num[i-1])i++;
}
return list;
}
}
[Thoughts]
这里实现了nsum的code,测试4sum的OJ AC.