给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组。
注意:答案中不可以包含重复的三元组。
Given an array nums of n integers, are there elements a, b, c in nums such that a+b+c = 0 ?
Find all unique triplets in the array which gives the sum of zero.
Notice that the solution set must not contain duplicate triples.
Example 1:
Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]
Example 2:
Input: nums = []
Output: []
Example 3:
Input: nums = [0]
Output: []
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/3sum
解决思路:
对于求三个数的和为0的 triplets,这三个数中,除了全部都是0的情况之外,肯定会有至少一个负数,一个正数,另外的一个可能是正数,也可能是负数,或者是0。
首先,思路是先将一个数固定下来,再去找另外的两个数,我们这里固定的数就是三元组中最小的那个负数。然后,另外两个数的和就是这个负数的相反数。根据这个特点,在数组中去寻找符合 a + b = -fix的那两个数,找到后,加上我们所固定的fix这个数,就组成了一个三元组。
先对数组进行排序,然后遍历数组,因为求的是一个三元组,所以遍历到倒数第三个元素就可以了。在遍历的过程中,先进行剪枝优化,遇到正数的时候,就可以跳数循环了。因为,当fix的最小的数都是正数的话,后面两个更大的数肯定都是正数,就不会出现合为0的这种情况了。然后,还要加上对于重复元素的判断,我们在fix一个元素的时候,如果发现在这个元素和前一位已经fix过的元素相等的时候,我就continue掉。
对于遍历到的数,用0减去这个fix的数得到一个target,然后只需要找到两个数之和等于target的二元组即可。这里可以使用两个指针分别指向fix数字之后开始的数组首位两个数,如果两个数之和就是target,则将这两个数和fix的那个数一起放入triplets中。放入triplets中之后,就要检查和跳过和这两个数字重复的数字了,继续进行判断。
最后,如果两个数的和小于target,就将左边那个指针first右移一位,否则,指针将右边的指针last左移一位,再继续进行判断即可。
代码如下:
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> result;
sort(nums.begin(), nums.end());
int size = nums.size();
if (nums.empty() || nums.back() < 0 || nums.front() > 0)
{
return {};
}
for (int i = 0; i < size - 2; i++)
{
if (nums[i] > 0)
{
break;
}
// if (nums[j] == nums[i] && j < size - 1)
// {
// continue; 这里j = i + 1,写代码时写的BUG判断,实际判断重复肯定是向前判定的。
// }
if(i > 0 && nums[i] == nums[i-1])
{
continue;
}
int fix = nums[i];
int target = 0 - fix;
int first = i + 1;
int last = size - 1;
while (first < last)
{
if (nums[first] + nums[last] == target)
{
result.push_back( {nums[i], nums[first], nums[last]} );
while (first < last && nums[first] == nums[first + 1])
{
first++;
}
while (first < last && nums[last] == nums[last - 1])
{
last--;
}
first++;
last--;
}
else if (nums[first] + nums[last] < target)
{
first++;
}
else
{
last--;
}
// if (nums[first] + nums[last] < target)
// {
// first++; 这个也是写的BUG,不能两个if分别再去判断了,因为第一if中first和last的值已经修改了。
// 如果[0,0,0],这里就报错了,这个判断应该是if -- else if --- else
// }
// if (nums[first] + nums[last] > target)
// {
// last--;
// }
}
}
return result;
}
};
本文介绍了一种解决三数之和为零问题的有效算法。通过对数组进行排序,并使用双指针技巧来查找所有不重复的三元组,使得它们的总和为零。文章详细解释了算法步骤,并提供了实现代码。
1220

被折叠的 条评论
为什么被折叠?



