当前位置:网站首页>18. 四数之和(15加强版)【ans.add(Arrays.asList(nums[i], nums[j], nums[l], nums[r]))】
18. 四数之和(15加强版)【ans.add(Arrays.asList(nums[i], nums[j], nums[l], nums[r]))】
2022-07-16 12:12:00 【LIZHUOLONG1】
18. 四数之和
给你一个由 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
-109 <= nums[i] <= 109
-109 <= target <= 109
思路:
- 10^9: 10位数
- int 占4个字节 32位二进制,2^31-1=2147483647,10位数。
nums[i]+nums[i+1]没有超出int的范围。nums[i]+nums[i+1]+nums[i+2]+nums[i+3]就超出int的范围了。
Java代码1:未剪枝
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> ans = new ArrayList<List<Integer>>();
int len = nums.length;
if (len < 4) return ans; //剪枝
Arrays.sort(nums);
for (int i = 0; i < len; ++i) {
if (i > 0 && nums[i] == nums[i - 1]) continue; //去重
for (int j = i + 1; j < len; ++j) {
if (j > i + 1 && nums[j] == nums[j - 1]) continue; //去重
int l = j + 1;
int r = len - 1;
while (l < r) {
long sum = (long) nums[i] + nums[j] + nums[l] +nums[r];
if (sum < target) ++l;
else if (sum > target) --r;
else {
ans.add(Arrays.asList(nums[i], nums[j], nums[l], nums[r]));
while (l < r && nums[l] == nums[l + 1]) ++l; //去重
while (l < r && nums[r] == nums[r - 1]) --r; //去重
++l;
--r;
}
}
}
}
return ans;
}
}
执行用时:17 ms, 在所有 Java 提交中击败了34.81%的用户
内存消耗:41.4 MB, 在所有 Java 提交中击败了94.75%的用户
Java代码2:剪枝
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> ans = new ArrayList<List<Integer>>();
int len = nums.length;
if (len < 4) return ans; //剪枝
Arrays.sort(nums);
for (int i = 0; i < len - 3; ++i) {
if (i > 0 && nums[i] == nums[i - 1]) {
//去重
continue;
}
if ((long) nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3] > target) {
//剪枝
break;
}
if ((long) nums[i] + nums[len - 1] + nums[len - 2] + nums[len - 3] < target) {
//剪枝
continue;
}
for (int j = i + 1; j < len - 2; ++j) {
if (j > i + 1 && nums[j] == nums[j - 1]) {
//去重
continue;
}
if ((long) nums[i] + nums[j] + nums[j + 1] + nums[j + 2] > target) {
//剪枝
break;
}
if ((long) nums[i] + nums[j] + nums[len - 1] + nums[len - 2] < target) {
//剪枝
continue;
}
int l = j + 1;
int r = len - 1;
while (l < r) {
long sum = (long) nums[i] + nums[j] + nums[l] +nums[r];
if (sum < target) ++l;
else if (sum > target) --r;
else {
ans.add(Arrays.asList(nums[i], nums[j], nums[l], nums[r]));
while (l < r && nums[l] == nums[l + 1]) ++l; //去重
while (l < r && nums[r] == nums[r - 1]) --r; //去重
++l;
--r;
}
}
}
}
return ans;
}
}
执行用时:2 ms, 在所有 Java 提交中击败了100.00%的用户
内存消耗:42 MB, 在所有 Java 提交中击败了25.55%的用户
通过测试用例:291 / 291
边栏推荐
- The United States claimed that it did not "block" Russian agricultural products. Russian lawmakers criticized the United States as the "initiator" of the crisis
- Interviewer: why does redis need sentinels? How should I answer?
- Stc8h development (XIV): I2C drive rx8025t high-precision real-time clock chip
- 爱立信以侵权为由要求禁售,苹果回返美国反诉,这一幕如此熟悉
- 如何解决8080端口被占用
- ipfs记录
- PostgreSQL source code (5) buffer management
- Multitree -- > b tree and b+ tree
- 【CVA估值训练营】如何快速读懂上市公司年报(第一讲)
- 安全的终止2nodeos的运行
猜你喜欢

Web性能测试工具之ab入门篇

JS Base64 to picture

The first China Digital Collection conference was successfully held

【论文阅读】Deep Transformer Q-Networks for Partially Observable Reinforcement Learning

多叉树--->B树和B+树

Detailed explanation of file parsing vulnerability

Introduction to leetcode special dynamic planning

SWD/JTAG Communication Failure和No Target Connected

NJUPT 《信安数基》第 11 章解题攻略

【论文笔记】—目标检测—YOLOv1—2016-CVPR
随机推荐
漏洞扫描的原理与应用
Architecture layering of standard web system of Architecture Series
thymeleaf介绍与简单应用
深度学习小记 - 正则化,优化器,线性回归,逻辑斯蒂回归
二 配置目标make menuconfig的执行过程分析
The United States claimed that it did not "block" Russian agricultural products. Russian lawmakers criticized the United States as the "initiator" of the crisis
MFC|框架下按钮的自绘
面试官:为什么 Redis 要有哨兵?我该怎么回答?
【论文笔记】—特征可视化—ZFNet—2014-ECCV
leetcode专项 动态规划入门
An excellent graphical tool for information collection maltego
给定一个整数数组nums和一个目标值target,在该数组中找出 和为 目标值的那两个整数,并返回他们的数组下标。
Introduction to leetcode special dynamic planning
[actual combat] 1382- Yiwen owns your puppeter crawler application
六 makefile.build的分析
Etcd database source code analysis -- etcdserver bootstrap to remove v2store
How to multiply bank revenue through customer value analysis
AB introduction to web performance testing tools
[paper reading] deep transformer q-networks for partially observable reinforcement learning
About the problem that the valueint of cjson exceeds the integer range