15.3Sum (Two-Pointers)

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.

思路:以下解法,固定两个数,用二分法查找第三个数,时间复杂度O(n2logn),结果超时

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> result;
        vector<int> item;
        int i, j, target;
        int size = nums.size();
        if(size < 3) return result;

        sort(nums.begin(), nums.end());
        for(i = 0; i < size-2; i++){
            for(j = i+1; j < size-1; j++){
                target = 0-nums[i]-nums[j];
                if(binarySearch(nums, j+1, size-1, target)){
                    item.clear();
                    item.push_back(nums[i]);
                    item.push_back(nums[j]);
                    item.push_back(target);
                    result.push_back(item);
                }
            }
        }
        return result;
    }
    bool binarySearch(vector<int>& nums, int start, int end, int target){
        if(start == end) return (nums[start]==target?true:false);
        int mid = (start + end) >> 1;
        if(nums[mid] == target) return true;
        else if(nums[mid] < target) return binarySearch(nums, mid+1, end, target);
        else return binarySearch(nums, start, mid, target);
    }
};

以下方法OK,注意要跳过重复的数字

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        int size = nums.size();
        if(size < 3) return result;

        sort(nums.begin(), nums.end());
        find(nums, 1, size-1, -nums[0]);
        for(int i = 1; i < size-2; i++){
            if(nums[i]!=nums[i-1]) find(nums, i+1, size-1, -nums[i]);
        }
        return result;
    }
    void find(vector<int>& nums, int start, int end, int target){
        if(start >= end) return;
        int sum = nums[start]+nums[end];
        if(sum == target){
            item.clear();
            item.push_back(-target);
            item.push_back(nums[start]);
            item.push_back(nums[end]);
            result.push_back(item);
            start++;
            while(start!= end && nums[start] == nums[start-1]) start++;
            if(start == end) return;
            end--;
            while(end!=start && nums[end] == nums[end+1]) end--;
            if(end == start) return;
            find(nums, start, end, target);
        }
        else if(sum>target){
            end--;
            while(end!=start && nums[end] == nums[end+1]) end--;
            if(end == start) return;
            find(nums, start, end, target);
        }
        else{
            start++;
            while(start!= end && nums[start] == nums[start-1]) start++;
            if(start == end) return;
            find(nums, start, end, target);
        }
    }

private:
    vector<vector<int>> result;
    vector<int> item;
};
时间: 2024-10-03 14:55:46

15.3Sum (Two-Pointers)的相关文章

*15. 3Sum (three pointers to two pointers), hashset

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. Note: The solution set must not contain duplicate triplets. Example: Given array nums =

1. Two Sum&amp;&amp;15. 3Sum&amp;&amp;18. 4Sum

题目: 1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Given nums = [2, 7, 1

Leetcode之15. 3Sum (medium)

15. 3Sum (medium) 描述 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. Note: The solution set must not contain duplicate triplets. Example

15.3sum(Nsum总结)

如果不用hashmap解决2sum,而是把2sum看成nSum中的一部分,那么就需要用two pointers来解决问题. 2sum伪代码: 1. 先对给定的数组进行排序 2. walker指第一个元素,runner指最后一个元素 3.当walker小于runner: 1)如果walker和runner的和等于target,那么: (1)如果只需要一对解,记录答案,break (2)如果需要所有解,那么walker++, runner-- 2) else如果和小于target,walker++

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

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

题目描述: 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. 解题分析: 这道题注意一下几点即可: 1,先固定前两个数的位置,移动第三个数,这样的查找方式不会有数字组合的遗漏: 2,要考虑到数组元素有重复的情况下的处理. 3,若先为数组排

15. 3Sum java solutions

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