码迷,mamicode.com
首页 > 其他好文 > 详细

15.3Sum (Two-Pointers)

时间:2015-07-25 08:20:07      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

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;
};

 

15.3Sum (Two-Pointers)

标签:

原文地址:http://www.cnblogs.com/qionglouyuyu/p/4675179.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!