引言:
Two Sum類問題屬於對撞型雙指針問題,這類問題的思路不難,但是變形以及問法比較多,這裏將部分題目做歸納總結。
在Two Sum問題中,需要用的預處理爲排序。但有的時候需要返回索引的位置,就需要設計數據結構先保存索引信息,再進行排序。如果不想這樣麻煩的話,可以藉助哈希表這個常用的數據結構來代替雙指針。
題解:
1.兩數之和 II Two Sum問題的基本變形,在一組整數中找出多少對整數,大於給定目標值。
基本思路:排序預處理 + 雙指針
Code:
public class Solution {
/**
* @param nums: an array of integer
* @param target: an integer
* @return: an integer
*/
public int twoSum2(int[] nums, int target) {
// Write your code here
if (nums == null || nums.length == 0) {
return 0;
}
Arrays.sort(nums);
int count = 0;
int i = 0;
int j = nums.length - 1;
while (i < j) {
if (nums[i] + nums[j] > target) {
count += j - i;
j--;
} else {
i++;
}
}
return count;
}
}
2.3Sum Closest
基本思路:2Sum Closet的變形。
這裏有抽出函數的寫法和不抽出函數的寫法,
另外nums[A] + nums[B] + nums[C] = target + closet.
特別注意這裏函數的寫法和初始化,推薦用函數寫法。
Code:
public class Solution {
public int threeSumClosest(int[] nums, int target) {
if (nums == null || nums.length < 3) {
return -1;
}
Arrays.sort(nums);
int closet = nums[0] + nums[1] + nums[2] - target;
for (int i = 0; i < nums.length - 2; i++) {
int left = i + 1;
int right = nums.length - 1;
int curr = twoSumCloset(nums, target - nums[i], left, right);
closet = Math.abs(closet) > Math.abs(curr) ? curr : closet;
}
return target + closet;
}
private int twoSumCloset(int[] nums, int target, int left, int right) {
int diff = nums[left] + nums[left + 1] - target;
int start = left;
int end = right;
while (start < end) {
int curr = nums[start] + nums[end] - target;
if (Math.abs(curr) < Math.abs(diff)) {
diff = curr;
}
if (nums[start] + nums[end] < target) {
start++;
} else {
end--;
}
}
return diff;
}
}
3.4Sum
基本思路同3Sum…
分析:這裏只是問法不同,返回所有的元組,另外還有個去重的技術,這裏的寫法就沒有抽取函數了。
Code:
public class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> rst = new ArrayList<List<Integer>>();
if (nums == null || nums.length == 0) {
return rst;
}
Arrays.sort(nums);
for (int i = 0; i < nums.length - 3; i++) {
if (i != 0 && nums[i] == nums[i - 1]) {
continue;
}
for (int j = i + 1; j < nums.length - 2; j++) {
if (j != i + 1 && nums[j] == nums[j - 1]) {
continue;
}
int left = j + 1;
int right = nums.length - 1;
while (left < right) {
int sum = nums[i] + nums[j] + nums[left] + nums[right];
if (sum == target) {
ArrayList<Integer> tmp = new ArrayList<>();
tmp.add(nums[i]);
tmp.add(nums[j]);
tmp.add(nums[left]);
tmp.add(nums[right]);
rst.add(tmp);
left++;
right--;
while (left < right && nums[left] == nums[left - 1]) {
left++;
}
while (left < right && nums[right] == nums[right + 1]) {
right--;
}
} else if (sum < target) {
left++;
} else {
right--;
}
}
}
}
return rst;
}
}
4.Two Sum - Difference equals to target。
在整數數組中,找到兩數之差等於目標值的索引位置。
基本思路:由於要找索引,排序會破壞索引,所以我自己用的哈希表。
Code:
public class Solution {
/*
* @param nums an array of Integer
* @param target an integer
* @return [index1 + 1, index2 + 1] (index1 < index2)
*/
public int[] twoSum7(int[] nums, int target) {
// write your code here
int[] res = new int[2];
if (nums == null || nums.length == 0) {
return res;
}
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(nums[i] + target)) {
res[0] = map.get(nums[i] + target) + 1;
res[1] = i + 1;
return res;
}
else if (map.containsKey(nums[i] - target)) {
res[0] = map.get(nums[i] - target) + 1;
res[1] = i + 1;
return res;
} else {
map.put(nums[i], i);
}
}
return res;
}
}
然而這道問題可以用我引言中提到的方法,設計數據結構來存儲索引信息,再來雙指針(快慢型雙指針)。
Code
5.三角形計數
基本思路:方法和問題1類似 (兩邊之和大於第三邊)
Code:
public class Solution {
/**
* @param S: A list of integers
* @return: An integer
*/
public int triangleCount(int S[]) {
// write your code here
int left = 0, right = S.length - 1;
int ans = 0;
Arrays.sort(S);
for(int i = 0; i < S.length; i++) {
left = 0;
right = i - 1;
while(left < right) {
if(S[left] + S[right] > S[i]) {
ans = ans + (right - left);
right --;
} else {
left ++;
}
}
}
return ans;
}
}
6.4Sum II
問題描述:給4個數組,問在這四個數組中各取一個數,構造成四元組。使其和等於目標值(0).
分析:2Sum的變形,藉助哈希表,將時間複雜度降到O(n^2)。
Code:
public class Solution {
public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
Map<Integer, Integer> map = new HashMap<>();
int count = 0;
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < B.length; j++) {
map.put(A[i] + B[j], map.getOrDefault(A[i] + B[j], 0)+ 1);
}
}
for (int i = 0; i < C.length; i++) {
for (int j = 0; j < D.length; j++) {
int target = -1 * (C[i] + D[j]);
count += map.getOrDefault(target, 0);
}
}
return count;
}
}