1.Two Sum
Description: 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
public class Solution {
public int[] twoSum(int[] nums, int target) {
if(nums==null||nums.length==0){
return null;
}
//存储结果
int[] res=new int[2];
Map<Integer,Integer> map=new HashMap<>();
for(int i=0;i<nums.length;i++){
map.put(nums[i], i);
}
for(int i=0;i<nums.length;i++){
if(map.containsKey(target-nums[i])&&i!=map.get(target-nums[i])){
res[0]=i+1;
res[1]=map.get(target-nums[i])+1;
break;
}
}
return res;
}
}
Description: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)
public List<List<Integer>> threeSum(int[] num) {
if(num==null)
return null;
List<List<Integer>> res=new ArrayList<List<Integer>>();
Arrays.sort(num);
for(int i=0;i<num.length;i++){
ArrayList<Integer> temp=new ArrayList<Integer>();
if(i>0&&num[i]==num[i-1])
continue;
int sum=-num[i];
int p=i+1;
int q=num.length-1;
while(p<q){
if(num[p]+num[q]==sum){
temp.add(num[i]);
temp.add(num[p]);
temp.add(num[q]);
res.add(temp);
temp=new ArrayList<Integer>();
while(p+1<q&&num[p+1]==num[p])
p++;
p++;
while(q-1>p&&num[q]==num[q-1])
q--;
q--;
}
else if(num[p]+num[q]>sum){
q--;
}
else
p++;
}
}
return res;
}
Description: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).
public int threeSumClosest(int[] num, int target) {
Arrays.sort(num);
int res=Integer.MAX_VALUE;
int res_res=Integer.MAX_VALUE;
for(int i=0;i<num.length;i++){
if(i>0&&num[i]==num[i-1])
continue;
int sum=target-num[i];
int p=i+1;
int q=num.length-1;
int sub=Integer.MAX_VALUE;
int res_sub=Integer.MAX_VALUE;
while(p<q){
int ab = 0;
if(num[p]+num[q]==sum)
return target;
else if(num[p]+num[q]>sum){
ab=Math.abs(num[p]+num[q]+num[i]-target);
if(sub>ab){
sub=ab;
res_sub=num[p]+num[q]+num[i];
}
q--;
}
else{
ab=Math.abs(num[p]+num[q]+num[i]-target);
if(sub>ab){
sub=ab;
res_sub=num[p]+num[q]+num[i];
}
p++;
}
}
if(res>sub){
res=sub;
res_res=res_sub;
}
}
return res_res;
}
Description: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)
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> res=new ArrayList<List<Integer>>();
Arrays.sort(nums);
for(int i=0;i<nums.length-3;i++){
for(int j=i+1;j<nums.length-2;j++){
int p=j+1;
int q=nums.length-1;
while(p<q){
List<Integer> temp=new ArrayList<Integer>();
int tempSum=nums[i]+nums[j]+nums[p]+nums[q];
if(tempSum==target){
temp.add(nums[i]);
temp.add(nums[j]);
temp.add(nums[p]);
temp.add(nums[q]);
if(res.contains(temp)==false){
res.add(temp);
}
p++;
q--;
}
else if(tempSum>target){
q--;
}
else
p++;
}
}
}
return res;
}