【LeetCode】3Sum && 3Sum Closest && 4Sum

1、3Sum 
Total Accepted: 7974 Total Submissions: 47933 My Submissions
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)
2、3Sum Closest
 Total Accepted: 5564 Total Submissions: 20793 My Submissions
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).
3、4Sum
Total Accepted: 4789 Total Submissions: 22016 My Submissions
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)
        做这些题目之前,建议先理解 Two sum一题,并能给出详细的解题方案。具体可参见 LeetCode/Two Sum && Jobdu 题目1352:和为S的两个数字
        如果能理解Two sum并彻底明白解题方法,那么这三道题自然就迎刃而解了。
        所有的思路都是一样的,固定一个数,然后扫描数组之后的数字之和是否符合要求。
        需要注意的就是处理重复数据。

1、Java AC

public class Solution {
    public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
		ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
		int len = num.length;
		Arrays.sort(num);
		for (int i = 0; i < len - 2; i++) {
			if (i > 0 && num[i] == num[i - 1]) {
				continue;
			}
			getTargetNum(list, num, i, i + 1, len - 1, len);
		}
		return list;
	}
	public void getTargetNum(ArrayList<ArrayList<Integer>> list,
			int[] num, int first, int low, int high, int len) {
		int target = num[first] * -1;
		ArrayList<Integer> numList = new ArrayList<Integer>();
		while (low < high) {
			if (num[low] + num[high] > target) {
				high--;
			} else if (num[low] + num[high] < target) {
				low++;
			} else {
				numList.add(num[first]);
				numList.add(num[low]);
				numList.add(num[high]);
				list.add(new ArrayList<Integer>(numList));
				numList.clear();
				int tempLow = low;
				int tempHigh = high;
				low++;
				high--;
				while (low < len && num[low] == num[tempLow]) {
					low++;
				}
				while (high >= 0 && num[high] == num[tempHigh]) {
					high--;
				}
			}
		}
	}
}
2、Java AC

public class Solution {
    public int closet;
	public int threeSumClosest(int[] num, int target) {
		int len = num.length;
		Arrays.sort(num);
		closet = num[0] + num[1] + num[2];
		for (int i = 0; i < len - 2; i++) {
			if (i > 0 && num[i] == num[i - 1]) {
				continue;
			}
			getTargetNum(num, i, i + 1, len - 1, len, target);
		}
		return closet;
	}
	public void getTargetNum(int[] num, int first, 
				int low, int high, int len, int target) {
		int tempValue = 0;
		while (low < high) {
			tempValue = num[first] + num[low] + num[high];
			if (Math.abs(tempValue - target) < Math.abs(closet - target)) {
				closet = tempValue;
			}
			if (tempValue > target) {
				int tempHigh = high;
				high--;
				while (high >= 0 && num[high] == num[tempHigh]) {
					high--;
				}
			} else if (tempValue < target) {
				int tempLow = low;
				low++;
				while (low < len && num[low] == num[tempLow]) {
					low++;
				}
			} else {
				return;
			}
		}
	}
}
3、Java AC

public class Solution {
    public ArrayList<ArrayList<Integer>> fourSum(int[] num, int target) {
		ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
		int len = num.length;
		Arrays.sort(num);
		for (int i = 0; i < len - 3; i++) {
			if (i > 0 && num[i] == num[i - 1]) {
				continue;
			}
			for (int j = i+1; j < len-2; j++) {
				if (j > i+1 && num[j] == num[j - 1]) {
					continue;
				}
				getTargetNum(list, num, i, j, j + 1, len - 1, len, target);
			}
		}
		return list;
	}
	public void getTargetNum(ArrayList<ArrayList<Integer>> list,
			int[] num, int first, int second, 
			int low, int high, int len, int target) {
		ArrayList<Integer> numList = new ArrayList<Integer>();
		int tempValue = (num[first] + num[second]);
		while (low < high) {
			if (tempValue + num[low] + num[high] > target) {
				high--;
			} else if (tempValue + num[low] + num[high] < target) {
				low++;
			} else {
				numList.add(num[first]);
				numList.add(num[second]);
				numList.add(num[low]);
				numList.add(num[high]);
				list.add(new ArrayList<Integer>(numList));
				numList.clear();
				int tempLow = low;
				int tempHigh = high;
				low++;
				high--;
				while (low < len && num[low] == num[tempLow]) {
					low++;
				}
				while (high >= 0 && num[high] == num[tempHigh]) {
					high--;
				}
			}
		}
	}
}

你可能感兴趣的:(【LeetCode】3Sum && 3Sum Closest && 4Sum)