18:四数之和
This commit is contained in:
parent
e820af55ab
commit
4c5e8ad8e9
116
src/main/java/leetcode/editor/cn/FourSum.java
Normal file
116
src/main/java/leetcode/editor/cn/FourSum.java
Normal file
@ -0,0 +1,116 @@
|
||||
//给你一个由 n 个整数组成的数组 nums ,和一个目标值 target 。请你找出并返回满足下述全部条件且不重复的四元组 [nums[a], nums[
|
||||
//b], nums[c], nums[d]] :
|
||||
//
|
||||
//
|
||||
// 0 <= a, b, c, d < n
|
||||
// a、b、c 和 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)
|
||||
|
||||
}
|
@ -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
36
src/main/java/leetcode/editor/cn/doc/content/FourSum.md
Normal file
36
src/main/java/leetcode/editor/cn/doc/content/FourSum.md
Normal file
@ -0,0 +1,36 @@
|
||||
<p>给你一个由 <code>n</code> 个整数组成的数组 <code>nums</code> ,和一个目标值 <code>target</code> 。请你找出并返回满足下述全部条件且不重复的四元组 <code>[nums[a], nums[b], nums[c], nums[d]]</code> :</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= a, b, c, d < 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> </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> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 200</code></li>
|
||||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>-10<sup>9</sup> <= target <= 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
Loading…
Reference in New Issue
Block a user