Problem:
Given an array S of n integers, is 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 is 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: First sort the array, time complexity O (log (n)), and then set a number of positions, find the other two numbers and equal to the-nums[i] combination, because the arrays are ordered, so you can go from both sides to the middle, when the result is greater than 0 back to step backward, Otherwise the front further, Time complexity O (n^2), so the time complexity of O (n^2)
The main idea: to give a set of arrays, requires that all and 0 of the number combination, requires that the number combination can not be repeated, and in ascending order
Problem-solving ideas: see solution.
Java source code (spents 437ms):
public class Solution {public list<list<integer>> threesum (int[] nums) {List<list<integer> ;> res = new arraylist<list<integer>> (); int len=nums.length; if (len<3) return res; Arrays.sort (Nums); for (int i=0;i<len;i++) {if (nums[i]>0) break; if (i>0 && nums[i]==nums[i-1]) continue; int begin=i+1,end=len-1; while (begin<end) {int sum=nums[i]+nums[begin]+nums[end]; if (sum==0) {list<integer> List = new arraylist<integer> (); List.add (Nums[i]); List.add (Nums[begin]); List.add (Nums[end]); Res.add (list); begin++;end--; while (Begin<end && nums[begin]==nums[begin-1]) begin++; while (Begin<end && nums[end]==nums[end+1]) end--; }else if (sum>0) end--; else begin++; }} return res; }}
C Language Source code (spents 48ms):
/** * Return An array of arrays of size *returnsize. * Note:the returned array must is 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<r) {while (L<r && nums[r]>=temp) r--; if (l<r) nums[l]=nums[r]; while (L<r && nums[l]<=temp) l++; if (l<r) nums[r]=nums[l]; } nums[l]=temp; QuickSort (NUMS,FIRST,L-1); QuickSort (nums,l+1,end);} int** threesum (int* nums, int numssize, int* returnsize) {int i,sum,top=-1,begin,end; int** res= (int**) malloc (sizeof (int*) * (numssize* (numsSize-1) * (numsSize-2))/6); if (numssize<3) {*returnsize=0; return res; } quickSort (nums,0,numssize-1); for (i=0;i<numssize;i++) {if (nums[i]>0) break; if (i>0 && nums[i]==nums[i-1]) continue; begin=i+1;end=numssize-1; while (begin<end) {sum=nums[I]+nums[begin]+nums[end]; if (sum==0) {top++; res[top]= (int*) malloc (sizeof (int.)); Res[top][0]=nums[i];res[top][1]=nums[begin];res[top][2]=nums[end]; begin++;end--; while (Begin<end && nums[begin]==nums[begin-1]) begin++; while (Begin<end && nums[end]==nums[end+1]) end--; }else if (sum>0) end--; else begin++; }} *returnsize=top+1; return res;}
C + + source code (66MS):
Class Solution {public:vector<vector<int>> Threesum (vector<int>& nums) {Vector<vector <int>> Res; int len=nums.size (); if (len<3) {return res; } sort (Nums.begin (), Nums.end ()); for (int i=0;i<len;i++) {if (nums[i]>0) break; if (i>0 && nums[i]==nums[i-1]) continue; int begin=i+1,end=len-1; while (begin<end) {int sum=nums[i]+nums[begin]+nums[end]; if (sum==0) {vector<int> T; T.push_back (Nums[i]); T.push_back (Nums[begin]); T.push_back (Nums[end]); Res.push_back (t); begin++;end--; while (Begin<end && nums[begin]==nums[begin-1]) begin++; while (Begin<end && nums[end]==nums[end+1]) end--; }else if (sum>0) {end--; }else begin++; }} return res; }};
Python source code (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 begin<end and nums[begin]==nums[begin-1]:begin+=1 while Begin<end and Nums[end] = = nums[end+1]:end-=1 elif sum>0:end-=1 else:begin+=1 return res
Leetcode 3Sum (C,c++,java,python)