[LeetCode] 15. 3Sum

Given an array S of n integers, are there elements abc in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note: The solution set must not contain duplicate triplets.

For example, given array S = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
  [-1, 0, 1],
  [-1, -1, 2]
]
 1 class Solution {
 2 public:
 3     vector<vector<int>> threeSum(vector<int>& nums) {
 4         vector<vector<int>> ret;
 5
 6         sort(nums.begin(), nums.end());
 7
 8         for (int i = 0; i < nums.size(); i++){
 9             // if i is  scanced already, skip it
10             if (i > 0 && nums[i] == nums[i-1]) continue;
11             int left = i + 1;
12             int right = nums.size() - 1;
13             while (left < right){
14                 if ((nums[left] + nums[right]) < -nums[i]){
15                     left++;
16                 }else if ((nums[left] + nums[right]) > -nums[i]){
17                     right--;
18                 }else{
19                     vector<int> tmp;
20                     tmp.push_back(nums[i]);
21                     tmp.push_back(nums[left]);
22                     tmp.push_back(nums[right]);
23                     ret.push_back(tmp);
24                     left++;
25                     right--;
26                     // remove duplicates!
27                     while(nums[left] == nums[left - 1]) left++;
28                     while(nums[right] == nums[right+1]) right--;
29                 }
30             }
31         }
32
33         return ret;
34     }
35 };
时间: 2024-12-31 04:00:41

[LeetCode] 15. 3Sum的相关文章

LeetCode 15. 3Sum(三数之和)

Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate triplets. For example, given array S = [-1,

[LeetCode][15]3Sum解析与快速排序算法-Java实现

Q: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate triplets. For example, given array S = [-

leetcode 15. 3Sum 双指针

题目链接 给n个数, 找出三个数相加结果为0的所有的组, 不可重复. 用双指针的思想,O(n^2)暴力的找, 注意判重复. 1 class Solution { 2 public: 3 vector<vector<int>> threeSum(vector<int>& nums) { 4 int sz = nums.size(); 5 vector <vector<int> > ans; 6 vector <int> tmp;

leetCode 15. 3Sum (3个数) 解题思路和方法

3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The

[LeetCode] 15. 3Sum Java

题目:Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate triplets. For example, given array S = [-

LeetCode 15 3Sum(3个数的和)

翻译 给定一个有n个整数的数组S,是否存在三个元素a,b,c使得a+b+c=0? 找出该数组中所有不重复的3个数,它们的和为0. 备注: 这三个元素必须是从小到大进行排序. 结果中不能有重复的3个数. 例如,给定数组S={-1 0 1 2 -1 4},一个结果集为: (-1, 0, 1) (-1, -1, 2) 原文 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? F

LeetCode #15 3Sum (M)

[Problem] Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)

LeetCode 15 3Sum (C,C++,Java,Python)

Problem: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)

Java [leetcode 15] 3Sum

问题描述: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The