当前位置:网站首页>【萌新解题】四数之和
【萌新解题】四数之和
2022-07-17 21:35:00 【InfoQ】
题目描述
- 0 <= a, b, c, d < n
- a、b、c 和 d 互不相同
- nums[a] + nums[b] + nums[c] + nums[d] == target
解题说明
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
// 数组排序
Arrays.sort(nums);
// 数组长度
int len = nums.length;
// 结果集合
List<List<Integer>> ans = new ArrayList<>();
List<Integer> tmp = null;
for (int i=0; i<len; ++i) {
// 固定nums[i]
List<List<Integer>> res = threeSum(nums, i+1, target - nums[i]);
for (List<Integer> item : res) {
item.add(nums[i]);
ans.add(item);
}
// 跳过重复元素
while (i < len-1 && nums[i] == nums[i+1]) {
++i;
}
}
return ans;
}
/**
* 三数之和,输入数组已经排好序
* 在数组nums中找出所有和等于target的不重复三元组
*/
public List<List<Integer>> threeSum(int[] nums, int start, int target) {
// 数组长度
int len = nums.length;
// 结果集合
List<List<Integer>> ans = new ArrayList<>();
// 结果集合中的某一个三元组
List<List<Integer>> tmp = null;
for (int i = start; i < len; ++i) {
// 固定nums[i]作为第一个元素,通过twoSum得到所有满足条件的另外两个元素
// 然后一起组成一个三元组,注意,twoSum 入参中,数组开始的下标start要设置
// 为i+1,不能再包含 nums[i]
tmp = twoSum(nums, i + 1, target - nums[i]);
for (List<Integer> item : tmp) {
item.add(nums[i]);
// 找到符合条件的一个三元组,并添加到结果结合中
ans.add(item);
}
// 当出现类似 [1,1,2,2,2,3,3] 这样有重复元素的数组时,要跳过重复元素
while (i < len - 1 && nums[i] == nums[i + 1]) {
++i;
}
}
return ans;
}
/**
* 两数之和
* 输入数组nums已经排好序,无需重复排序,且下标从start开始
* 也就是找出从start开始到nums.length之间,所有满足和为target的不重复二元组
*/
public List<List<Integer>> twoSum(int[] nums, int start, int target) {
// 数组长度
int len = nums.length;
// 左指针,指向数组开头
int left = start;
// 右指针,指向数组尾部
int right = len - 1;
// 返回结果
List<List<Integer>> ans = new ArrayList<>();
// 开始循环
while (left < right) {
int sum = nums[left] + nums[right];
// 保存当前循环的第一个数,后面用于判重
int numLeft = nums[left];
// 保存当前循环的第二个数,后面用于判重
int numRight = nums[right];
if (target == sum) {
// 找到目标元素,保存到结果集中
List<Integer> tmp = new ArrayList<>();
tmp.add(nums[left]);
tmp.add(nums[right]);
ans.add(tmp);
// 当出现类似 [1,1,2,2,2,3,3] 这样有重复元素的数组时,要跳过重复元素
// 因为题目要求结果集合不重复
while (left < right && nums[left] == numLeft) {
left++;
}
// 当出现类似 [1,1,2,2,2,3,3] 这样有重复元素的数组时,要跳过重复元素
// 因为题目要求结果集合不重复
while (left < right && nums[right] == numRight) {
right--;
}
} else if (target < sum) {
// 当出现类似 [1,1,2,2,2,3,3] 这样有重复元素的数组时,要跳过重复元素
// 减少不必要的循环
while (left < right && nums[right] == numRight) {
right--;
}
} else if (target > sum) {
// 当出现类似 [1,1,2,2,2,3,3] 这样有重复元素的数组时,要跳过重复元素
// 减少不必要的循环
while (left < right && nums[left] == numLeft) {
left++;
}
}
}
return ans;
}
}

target - nums[i]class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
// 数组排序
Arrays.sort(nums);
// 数组长度
int len = nums.length;
Long targetLong = (long)target;
// 结果集合
List<List<Integer>> ans = new ArrayList<>();
List<Integer> tmp = null;
for (int i=0; i<len; ++i) {
// 固定nums[i]
List<List<Integer>> res = threeSum(nums, i+1, targetLong - nums[i]);
for (List<Integer> item : res) {
item.add(nums[i]);
ans.add(item);
}
// 跳过重复元素
while (i < len-1 && nums[i] == nums[i+1]) {
++i;
}
}
return ans;
}
/**
* 三数之和,输入数组已经排好序
* 在数组nums中找出所有和等于target的不重复三元组
*/
public List<List<Integer>> threeSum(int[] nums, int start, long target) {
// 数组长度
int len = nums.length;
// 结果集合
List<List<Integer>> ans = new ArrayList<>();
// 结果集合中的某一个三元组
List<List<Integer>> tmp = null;
for (int i = start; i < len; ++i) {
// 固定nums[i]作为第一个元素,通过twoSum得到所有满足条件的另外两个元素
// 然后一起组成一个三元组,注意,twoSum 入参中,数组开始的下标start要设置
// 为i+1,不能再包含 nums[i]
tmp = twoSum(nums, i + 1, target - nums[i]);
for (List<Integer> item : tmp) {
item.add(nums[i]);
// 找到符合条件的一个三元组,并添加到结果结合中
ans.add(item);
}
// 当出现类似 [1,1,2,2,2,3,3] 这样有重复元素的数组时,要跳过重复元素
while (i < len - 1 && nums[i] == nums[i + 1]) {
++i;
}
}
return ans;
}
/**
* 两数之和
* 输入数组nums已经排好序,无需重复排序,且下标从start开始
* 也就是找出从start开始到nums.length之间,所有满足和为target的不重复二元组
*/
public List<List<Integer>> twoSum(int[] nums, int start, long target) {
// 数组长度
int len = nums.length;
// 左指针,指向数组开头
int left = start;
// 右指针,指向数组尾部
int right = len - 1;
// 返回结果
List<List<Integer>> ans = new ArrayList<>();
// 开始循环
while (left < right) {
int sum = nums[left] + nums[right];
// 保存当前循环的第一个数,后面用于判重
int numLeft = nums[left];
// 保存当前循环的第二个数,后面用于判重
int numRight = nums[right];
if (target == sum) {
// 找到目标元素,保存到结果集中
List<Integer> tmp = new ArrayList<>();
tmp.add(nums[left]);
tmp.add(nums[right]);
ans.add(tmp);
// 当出现类似 [1,1,2,2,2,3,3] 这样有重复元素的数组时,要跳过重复元素
// 因为题目要求结果集合不重复
while (left < right && nums[left] == numLeft) {
left++;
}
// 当出现类似 [1,1,2,2,2,3,3] 这样有重复元素的数组时,要跳过重复元素
// 因为题目要求结果集合不重复
while (left < right && nums[right] == numRight) {
right--;
}
} else if (target < sum) {
// 当出现类似 [1,1,2,2,2,3,3] 这样有重复元素的数组时,要跳过重复元素
// 减少不必要的循环
while (left < right && nums[right] == numRight) {
right--;
}
} else if (target > sum) {
// 当出现类似 [1,1,2,2,2,3,3] 这样有重复元素的数组时,要跳过重复元素
// 减少不必要的循环
while (left < right && nums[left] == numLeft) {
left++;
}
}
}
return ans;
}
}
边栏推荐
猜你喜欢

Keil环境下STM32定位hardfault位置方法和遇到的情况

华为无线设备配置动态负载均衡

Deep understanding of transaction isolation levels

Gradle introduction notes

Méthode de compilation de la courbe RPS d'O'Neill (originale par le Dr Tao)

Huawei technologies:jonathan Krolikowski | from design to deployment, zero contact deep reinforcement learning WLANs
![Luo Gu: p3092 [usaco13nov]no change G](/img/b9/8cacd3d4ae1cf014654e0204cb3a62.png)
Luo Gu: p3092 [usaco13nov]no change G

Qchartview overwrites the previous control when it is added in qgridlayout

Sliding window maximum problem

Huawei wireless device configuration intelligent roaming
随机推荐
ospf-LSA
CMAKE学习笔记
详解C语言文件操作
ClassNotFoundException:com. tongweb. geronimo. osgi. locator. ProviderLocator
C speech Young's matrix · left-hand string · judge whether the string is rotated
After 2000, he was hired as a special associate researcher of Nanjing University. He went to primary school at the age of 4 and was admitted to Nanjing University at the age of 14!
Overview report of Chinese AI medical imaging industry in 2022
Display module in pyGame
ShanDong Multi-University Training #3
深入理解事务隔离级别
手册不全,如何手工刨出TongWeb的监控信息?
How to avoid global index in pychart? How to cancel the indexing of a folder?
TDesign CompositionAPI 重构之路
ping 命令还能这么玩?
函數初認識-下
Addition, deletion, modification and query of database
ClassNotFoundException:com.tongweb.geronimo.osgi.locator.ProviderLocator
Redis 与 Mysql 的数据一致性
07--- Brewster point
The TLS connection failed due to the incorrect configuration of privatekeyentry in the keystore file