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:

  • 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:
    int b_search(vector<int> &n, int b, int e, int x) {
        int l = b;
        int r = e;
        while (l <= r) {
            int mid = l + ((r - l ) >> 2);
            if (n[mid] == x) {
                return mid;
            } else if (n[mid] > x) {
                r = mid - 1;
            } else {
                l = mid + 1;
            }
        }
        return -1;
    }
    /*
     *固定其中2点,找第3点,如果找到就添加到结果中
     * 每次固定左边的点,从右想走遍历右边的点,查找两个点的之间是否有满足的点
     * 注意左边的点要去掉重复的
     */
    vector<vector<int>> threeSum(vector<int>& nums) {
        int i,j,k;
        int size = nums.size();
        vector<vector<int>> res;
        sort(nums.begin(), nums.end());
        for (i=0; i<size-2; i++) {
            //过滤掉相同的左边点,防止出现重复的结果
            if (i > 0 && nums[i] == nums[i-1]) {
                continue;
            }
            for (j=size-1; j>i+1; j--) {
                if (j < size-1 && nums[j] == nums[j+1]) {
                    continue;
                }
                int s = b_search(nums, i+1, j-1, 0 - nums[i] - nums[j]);
                if (s != -1) {
                    vector<int> tmp;
                    tmp.push_back(nums[i]);
                    tmp.push_back(nums[s]);
                    tmp.push_back(nums[j]);
                    res.push_back(tmp);
                }
            }
        }
        return res;
    }
};
时间: 2024-11-16 23:43:08

3Sum的相关文章

Leetcode Array 16 3Sum Closest

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2

Coursera Algorithms week1 Interview Questions: 3Sum in quadratic time

题目要求: Design an algorithm for the 3-SUM problem that takes time proportional to n2 in the worst case. You may assume that you can sort the n integers in time proportional to n2 or better. 分析: <算法4>这本书提供的TwoSumFast解法为NlogN,ThreeSumFast解法为N2logN,根据课后练

No.016 3Sum Closest

16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume

LeetCode #16 3Sum Closest (M)

[Problem] Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array

[LeetCode]#13 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笔记:3Sum Closest

一.题目描述 二.解题技巧 该题与3Sum的要求类似,不同的是要求选出的组合的和与目标值target最接近而不一定相等.但实际上,与3Sum的算法流程思路相似,先是进行排序,然后顺序选择数组A中的下标为i的元素值作为组合中三个数的最小值,进而寻找另外两个更大的值,最后求出三个数的和.不过的地方在于这里是寻找最靠近给定值,寻找最靠近的值就无所有重复的事情了,所以可以不考虑夹逼的过程中的越过相同元素的过程,虽然越过相同的元素速度会快一些,但是代码长度也会加长. 这道题难的地方可能在于刚开始这种差的阈

[LeetCode][JavaScript]3Sum Closest

3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given arr

[LeetCode] 3Sum Closest

This problem is very similar to 3Sum. You only need to maintain a variable for the sum that is closet to target. Also, some corner cases need to be handled; for example, nums does not have more than 2 elements. The code is as follows, which is quite

Java [leetcode 16] 3Sum Closest

题目描述: Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S =

LeetCode 16 3sum closest

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2