18:四数之和

This commit is contained in:
huangge1199@hotmail.com 2021-10-14 21:43:57 +08:00
parent e820af55ab
commit 4c5e8ad8e9
5 changed files with 157 additions and 5 deletions

View File

@ -0,0 +1,116 @@
//给你一个由 n 个整数组成的数组 nums 和一个目标值 target 请你找出并返回满足下述全部条件且不重复的四元组 [nums[a], nums[
//b], nums[c], nums[d]]
//
//
// 0 <= a, b, c, d < n
// abc d 互不相同
// nums[a] + nums[b] + nums[c] + nums[d] == target
//
//
// 你可以按 任意顺序 返回答案
//
//
//
// 示例 1
//
//
//输入nums = [1,0,-1,0,-2,2], target = 0
//输出[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
//
//
// 示例 2
//
//
//输入nums = [2,2,2,2,2], target = 8
//输出[[2,2,2,2]]
//
//
//
//
// 提示
//
//
// 1 <= nums.length <= 200
// -10 <= nums[i] <= 10
// -10 <= target <= 10
//
// Related Topics 数组 双指针 排序 👍 976 👎 0
package leetcode.editor.cn;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
//18:四数之和
class FourSum {
public static void main(String[] args) {
//测试代码
Solution solution = new FourSum().new Solution();
// solution.fourSum(new int[]{1,0,-1,0,-2,2},0);
solution.fourSum(new int[]{0, 0, 0, 1000000000, 1000000000, 1000000000, 1000000000}, 1000000000);
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> result = new ArrayList<>();
if (nums == null || nums.length < 4) {
return result;
}
Arrays.sort(nums);
int length = nums.length;
long sum;
for (int i = 0; i < length - 3; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
sum = (long)nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3];
if (sum > target) {
break;
}
sum = (long)nums[i] + nums[length - 3] + nums[length - 2] + nums[length - 1];
if (sum < target) {
continue;
}
for (int j = i + 1; j < length - 2; j++) {
if (j > i + 1 && nums[j] == nums[j - 1]) {
continue;
}
sum = (long)nums[i] + nums[j] + nums[j + 1] + nums[j + 2];
if (sum > target) {
break;
}
sum = (long)nums[i] + nums[j] + nums[length - 2] + nums[length - 1];
if (sum < target) {
continue;
}
int start = j + 1;
int end = length - 1;
while (start < end) {
sum = (long)nums[i] + nums[j] + nums[start] + nums[end];
if (sum == target) {
result.add(Arrays.asList(nums[i], nums[j], nums[start], nums[end]));
while (start < end && nums[start] == nums[start + 1]) {
start++;
}
start++;
while (start < end && nums[end] == nums[end - 1]) {
end--;
}
end--;
} else if (sum < target) {
start++;
} else {
end--;
}
}
}
}
return result;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -51,7 +51,9 @@ public class MergeSortedArray {
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
if (m + n - m >= 0) System.arraycopy(nums2, 0, nums1, m, m + n - m);
if (m + n - m >= 0) {
System.arraycopy(nums2, 0, nums1, m, m + n - m);
}
Arrays.sort(nums1);
}
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,36 @@
<p>给你一个由 <code>n</code> 个整数组成的数组&nbsp;<code>nums</code> ,和一个目标值 <code>target</code> 。请你找出并返回满足下述全部条件且不重复的四元组&nbsp;<code>[nums[a], nums[b], nums[c], nums[d]]</code> </p>
<ul>
<li><code>0 &lt;= a, b, c, d&nbsp;&lt; n</code></li>
<li><code>a</code><code>b</code><code>c</code><code>d</code> <strong>互不相同</strong></li>
<li><code>nums[a] + nums[b] + nums[c] + nums[d] == target</code></li>
</ul>
<p>你可以按 <strong>任意顺序</strong> 返回答案 。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>nums = [1,0,-1,0,-2,2], target = 0
<strong>输出:</strong>[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>nums = [2,2,2,2,2], target = 8
<strong>输出:</strong>[[2,2,2,2]]
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 200</code></li>
<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
<li><code>-10<sup>9</sup> &lt;= target &lt;= 10<sup>9</sup></code></li>
</ul>
<div><div>Related Topics</div><div><li>数组</li><li>双指针</li><li>排序</li></div></div><br><div><li>👍 976</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long