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

Problem:

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)

Solution:

先对数组进行排序,时间复杂度O(log(n)),然后定好一个数的位置,查找另外两个数的和等于-nums[i]的组合,由于数组排好序了,所以可以从两边往中间走,当结果大于0的时候后边往后退一步,否则前边进一步,时间复杂度O(n^2),所以时间复杂度为O(n^2)

题目大意:

给一组数组,要求得出所有和为0的数字组合,要求数字组合不能重复出现,并且按照升序排列

解题思路:

见Solution.

Java源代码(用时437ms):

public class Solution {
    public List> threeSum(int[] nums) {
        List> res = new ArrayList>();
        int len=nums.length;
        if(len<3)return res;
        Arrays.sort(nums);
        for(int i=0;i0)break;
            if(i>0 && nums[i]==nums[i-1])continue;
            int begin=i+1,end=len-1;
            while(begin list = new ArrayList();
                    list.add(nums[i]);list.add(nums[begin]);list.add(nums[end]);
                    res.add(list);
                    begin++;end--;
                    while(begin0)end--;
                else begin++;
            }
        }
        return res;
    }
}

C语言源代码(用时48ms):

/**
 * Return an array of arrays of size *returnSize.
 * Note: The returned array must be malloced, assume caller calls free().
 */
void quickSort(int* nums,int first,int end){
    int temp,l,r;
    if(first>=end)return;
    temp=nums[first];
    l=first;r=end;
    while(l=temp)r--;
        if(l0)break;
        if(i>0 && nums[i]==nums[i-1])continue;
        begin=i+1;end=numsSize-1;
        while(begin0) end--;
            else begin++;
        }
    }
    *returnSize=top+1;
    return res;
}

C++源代码(66ms):

class Solution {
public:
    vector> threeSum(vector& nums) {
        vector> res;
        int len=nums.size();
        if(len<3){
            return res;
        }
        sort(nums.begin(),nums.end());
        for(int i=0;i0)break;
            if(i>0 && nums[i]==nums[i-1])continue;
            int begin=i+1,end=len-1;
            while(begin t;
                    t.push_back(nums[i]);
                    t.push_back(nums[begin]);
                    t.push_back(nums[end]);
                    res.push_back(t);
                    begin++;end--;
                    while(begin0){
                    end--;
                }else begin++;
            }
        }
        return res;
    }
};

Python源代码(407ms):

class Solution:
    # @param {integer[]} nums
    # @return {integer[][]}
    def threeSum(self, nums):
        res = []
        length=len(nums)
        if length<3:return res
        nums.sort()
        for i in range(length):
            if nums[i]>0:break
            if i>0 and nums[i]==nums[i-1]:continue
            begin=i+1;end=length-1
            while begin < end:
                sum=nums[i]+nums[begin]+nums[end]
                if sum==0:
                    tmp=[nums[i],nums[begin],nums[end]]
                    res.append(tmp)
                    begin+=1;end-=1
                    while begin0:end-=1
                else:begin+=1
        return res


你可能感兴趣的:(LeetCode)