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?
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 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)

经典方法,可惜我并没有想到这样写……

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        vector<vector<int>> result;

        int len = nums.size();
        for (int current = 0; current < len - 2&&nums[current]<=0;current++)
        {
            int front = current + 1, back = len - 1;
            while (front < back)
            {
                if (nums[current] + nums[front] + nums[back] < 0)
                    front++;
                else if (nums[current] + nums[front] + nums[back] > 0)
                    back--;
                else
                {
                    vector<int> v(3);
                        v.push_back(nums[current]);
                        v.push_back(nums[front]);
                        v.push_back(nums[back]);
                        result.push_back(v);
                        v.clear();
                    do {
                        front++;
                    } while (front < back&&nums[front - 1] == nums[front]);
                    do {
                        back--;
                    } while (front < back&&nums[back + 1] == nums[back]);
                }
            }
            while (current < len - 2 && nums[current + 1] == nums[current])
                current++;
        }
        return result;
    }
};

继续努力……

版权声明:本文为 NoMasp柯于旺 原创文章,未经许可严禁转载!欢迎访问我的博客:http://blog.csdn.net/nomasp

时间: 2024-10-03 13:45:27

LeetCode 15 3Sum(3个数的和)的相关文章

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 找出数组里面3个数的和等于指定值。

题目: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 so

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 双指针

题目链接 给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解析与快速排序算法-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

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 (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

[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 = [-