当前位置:网站首页>【萌新解题】三数之和
【萌新解题】三数之和
2022-07-17 00:11:00 【InfoQ】
题目描述
解题说明
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
//按照题目要求,是求和0的三元组,所以target传0即可
return threeSum(nums, 0);
}
/**
* 三数之和
* 在数组nums中找出所有和等于target的不重复三元组
*/
public List<List<Integer>> threeSum(int[] nums, int target) {
// 首先对数组排序,这是一切的前提
Arrays.sort(nums);
// 数组长度
int len = nums.length;
// 结果集合
List<List<Integer>> ans = new ArrayList<>();
// 结果集合中的某一个三元组
List<List<Integer>> tmp = null;
for (int i = 0; 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) {
// TODO
}
}
public class Solution {
/**
* 两数之和
* 输入数组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;
}
}
边栏推荐
- Mxnet network model (IV) Gan neural network
- Detailed explanation of errno
- 【文献阅读】Counting Integer Points in Parametric Polytopes Using Barvinok‘s Rational Functions
- Xcode11新建项目后的一些问题
- 安全多方计算体系架构及应用思考
- 基于深度学习的加密流量识别研究综述及展望
- A causal linear model to quantify edge unfairness for unfair edge prioritization
- Why do you spend 1.16 million to buy an NFT avatar in the library of NFT digital collections? The answer may be found by reviewing the "rise history" of NFT avatars
- Fairness in Semi-supervised Learning: Unlabeled Data Help to Reduce Discrimination
- IPFs mount to local disk
猜你喜欢

Leveraging Semi-Supervised Learning for Fairness using Neural Networks

touchID 和 FaceID~2

爭奪存量用戶關鍵戰,助力企業構建完美標簽體系丨01期直播回顧

边缘检测方法 -- 一阶边缘检测

Apt get update error: hash checksum does not match
![[literature reading] mcunet: tiny deep learning on IOT devices](/img/67/21e5c6b7cf95073850be4c7c20520c.png)
[literature reading] mcunet: tiny deep learning on IOT devices

One vs One Mitigation of Intersectional Bias

6章 性能平台GodEye源码分析-自定义拓展模块

PCA主成分分析(降维)过程推导

动手学深度学习---从全连接层到卷积层篇
随机推荐
开源项目丨 Taier 1.1 版本正式发布,新增功能一览为快
1章 性能平台GodEye源码分析-整体架构
4章 性能平台GodEye源码分析-监控模块
5G专网在智慧医疗中的应用
touchID 和 FaceID~1
随机森林的理解
A causal linear model to quantify edge unfairness for unfair edge prioritization
安全多方计算体系架构及应用思考
2章 性能平台GodEye源码分析-数据模块
ACE下载地址
nodejs-uuid
Why do you spend 1.16 million to buy an NFT avatar in the library of NFT digital collections? The answer may be found by reviewing the "rise history" of NFT avatars
Fisher线性判别分析Fisher Linear Distrimination
判断两个数组是否完全相等
CMTime简单介绍
VSCode中安装Go:tools failed to install.
Détection de bord de deuxième ordre laplacien de guassian Gaussian laplacien Operator
未成年人数字安全保护的问题与对策
Boost线程池
The following packages have unmet dependencies: deepin. com. wechat:i386 : Depends: deepin-wine:i386