【LeetCode】两数之和、三数之和、四数之和系列

两数之和

LeetCode两数之和链接

``````class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer,Integer> hash = new HashMap<Integer,Integer>();
for(int i = 0; i<nums.length; i++){
if(hash.containsKey(nums[i])){
return (new int[] { i, hash.get(nums[i])});
}
hash.put(target - nums[i], i);
}
return null;
}
}
``````

三数之和

LeetCode三数之和链接

``````class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if(nums == null || nums.length < 3) {
return res;
}
Arrays.sort(nums);
for(int i = 0; i < nums.length - 2; i++) {
if(i > 0 && nums[i] == nums[i - 1]) {
continue;
}
if(nums[i] + nums[i + 1] + nums[i + 2] > 0) {
break;
}
//双指针
int le = i + 1, ri = nums.length - 1;
while(le < ri) {
int sum = nums[i] + nums[le] + nums[ri];
if(sum == 0) {
le++;
while(le < ri && nums[le] == nums[le - 1]) {
le++;
}
//此处先减1再执行循环可以避免越界（这样处理边界更合适）
ri--;
while(le < ri && nums[ri] == nums[ri + 1]) {
ri--;
}
}else if(sum < 0) {
le++;
}else {
ri--;
}
}
}
return res;
}
}
``````

四数之和

LeetCode四数之和链接

``````class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> res = new ArrayList<>();
if(nums == null || nums.length < 4) {
return res;
}
Arrays.sort(nums);
int len = nums.length;
for(int i = 0; i < len - 3; i++) {
if(i > 0 && nums[i] == nums[i - 1]) {
continue;
}
if(nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3] > target) {
break;
}
if(nums[i] + nums[len - 3] + nums[len - 2] + nums[len - 1] < target) {
continue;
}

for(int j = i + 1; j < len - 2; j++) {
if(j > i + 1 && nums[j] == nums[j - 1]) {
continue;
}
if(nums[i] + nums[j] + nums[j + 1] + nums[j + 2] > target) {
break;
}
if(nums[i] + nums[j] + nums[len - 2] + nums[len - 1] < target) {
continue;
}

int left = j + 1, right = len - 1;
while(left < right) {
int sum = nums[i] + nums[j] + nums[left] + nums[right];
if(sum == target) {
while(left < right && nums[left] == nums[left + 1]) {
left++;
}
left++;
while(left < right && nums[right - 1] == nums[right]) {
right--;
}
right--;
}else if(sum < target) {
left++;
}else {
right--;
}
}
}
}
return res;
}
}
``````

四数相加Ⅱ

LeetCode四数相加Ⅱ链接

``````class Solution {
public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
int count = 0;
int N = A.length;
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
map.put(A[i] + B[j], map.getOrDefault(A[i] + B[j], 0) + 1);
}
}
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
count += map.getOrDefault(-C[i] - D[j], 0);
}
}
return count;
}
}
``````

最接近的三数之和

``````class Solution {
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int N = nums.length;
int closestSum = nums[0] + nums[1] + nums[2];
for(int i = 0; i < N - 2; i++) {
int le = i + 1, ri = N - 1;
while(le < ri) {
int threeSum = nums[i] + nums[le] + nums[ri];
if(Math.abs(threeSum - target) < Math.abs(closestSum - target)) {
closestSum = threeSum;
}
if(threeSum < target) {
le++;
}else if(threeSum > target) {
ri--;
}else {
return target;
}
}
}
return closestSum;
}
}
``````

原文作者：白鳯
原文地址: https://blog.csdn.net/weixin_44368437/article/details/110527999
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。