Leetcode Problem Solving report--3 Sum

Source: Internet
Author: User

title: 3 Numbers and questions
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)

Original title link address: https://leetcode.com/problems/3sum/
Analysis: Test instructions is from the given array of integers, find out three numbers and 0 of all possible combinations, the problem has two methods to solve:
1. Violence law: Through three for the loop to find all triplet of the given array and the case, and through the IF statement, will and 0 of the triplet record, and print out! The time complexity is 0 (n^3), inevitably tle, unable to accepted.
2. Pointer Shift method: The original array is sorted first, then left (l), the Right (R) (pointer) variable points to the first element of the given array, the 3 numbers and problems into 2 numbers and 2sum problem. Time complexity O (NLOGN).
the above method should pay attention to the last removal of the heavy operation!
Brute Force Java code (timeout cannot be accepted)

ImportJava.util.ArrayList;ImportJava.util.Arrays;ImportJava.util.Collection;ImportJava.util.HashSet;ImportJava.util.Set;ImportCom.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;ImportCom.sun.xml.internal.bind.v2.schemagen.xmlschema.List; Public  class threesum {    /** * @param args * *     Public Static void Main(string[] args) {//TODO auto-generated method stub        int[] s =New int[] {0,2,1,-3}; System.out.println ("A solution set is:"); Arraylist<arraylist<integer>> Listarray =NewArraylist<arraylist<integer>> (); Listarray = Threesum (s); for(inti =0; I < listarray.size ();        i++) {System.out.println (Listarray.get (i)); }    } Public StaticArraylist<arraylist<integer>>Threesum(int[] nums) {arraylist<arraylist<integer>> list =NewArraylist<arraylist<integer>> (); String string =""; arraylist<string> string1 =NewArraylist<string> ();int[] B =New int[3]; for(inti =0; i < Nums.length;i + +) { for(intj = i +1; J < Nums.length;j + +) { for(intK = j +1K < nums.length;k + +) {arraylist<integer> element =NewArraylist<integer> ();if(Nums[k] + nums[j] + nums[i] = =0) {string ="";                        Element.add (Nums[i]);                        Element.add (Nums[j]);                        Element.add (Nums[k]); b[0] = Nums[i]; b[1] = Nums[j]; b[2] = Nums[k];//avoid reduplicated ResultArrays.sort (b); for(intn =0N < B.length;n + +) {string = string + B[n]; }if(!string1.contains (String))                            {List.add (element);                        String1.add (string); }                    }                }            }        }returnList }}

End-to-end pointer shift method Java code (accepted)

     Public StaticArraylist<arraylist<integer>> Threesum (int[] nums) {arraylist<arraylist<integer>> list =NewArraylist<arraylist<integer>> (); Hashset<arraylist<integer>> ElementSet =NewHashset<arraylist<integer>> ();int sum=0; Arrays.sort (Nums); for(inti =0; I < nums.length-2; i++) {//directly Avoid reduplicated result            intleft = i +1;intright = Nums.length-1; while(Left < right) {sum= Nums[i] + nums[left] + nums[right];if(sum==0) {arraylist<integer> element =NewArraylist<integer> ();                    Element.add (Nums[i]);                    Element.add (Nums[left]); Element.add (Nums[right]);if(!elementset.contains (Element))                        {List.add (element);                    Elementset.add (Element);                    } left + +;                Right--; }Else if(sum<0) {left + +; }Else{Right--; }            }        }returnList }}

Test results:

 A Solution Set  is:[ -15, 1, +][ -15, 2,][ -15, 3, a][ -15, 4, one][ -15, 5, ten][ -15, 6, 9][ -15, 7, 8][ -14, 0, +][ -14, 1,][ -14, 2, a][ -14, 3, one][ -14, 4, ten][ -14, 5, 9][ -14, 6, 8][ -14, 7, 7][ -13,-1, +][ -13, 0,][ -13, 1, a][ -13, 2, one][ -13, 3, ten][ -13, 4, 9][ -13, 5, 8][ -13, 6, 7][ -12,-2, +][ -12,-1,][ -12, 0, a][ -12, 1, one][ -12, 2, ten][ -12, 3, 9][ -12, 4, 8][ -12, 5, 7][ -12, 6, 6][ -11, -3, +][ -11,-2,][ -11,-1,][ -11, 0, one][ -11, 1, ten][ -11, 2, 9][ -11, 3, 8][ -11, 4, 7][ -11, 5, 6][ -10, -4, +][ -10, -3,][ -10,-2,][ -10,-1, one][ -10, 0, ten][ -10, 1, 9][ -10, 2, 8][ -10, 3, 7][ -10, 4, 6][ -10, 5, 5][-9, -5, +][-9, -4,][-9, -3,][-9,-2, one][-9,-1, ten][-9, 0, 9][-9, 1, 8][-9, 2, 7][-9, 3, 6][-9, 4, 5][-8,-6, +][-8, -5,][-8, -4,][-8, -3, one][-8,-2, ten][-8,-1, 9][-8, 0, 8][-8, 1, 7][-8, 2, 6][-8, 3, 5][-8, 4, 4][-7,-7, +][-7,-6,][-7, -5,][-7, -4, one][-7, -3, ten][-7,-2, 9][-7,-1, 8][-7, 0, 7][-7, 1, 6][-7, 2, 5][-7, 3, 4][-6,-6,][-6, -5, one][-6, -4, ten][-6, -3, 9][-6,-2, 8][-6,-1, 7][-6, 0, 6][-6, 1, 5][-6, 2, 4][-6, 3, 3][ -5, -5, ten][ -5, -4, 9][ -5, -3, 8][ -5,-2, 7][ -5,-1, 6][ -5, 0, 5][ -5, 1, 4][ -5, 2, 3][ -4, -4, 8][ -4, -3, 7][ -4,-2, 6][ -4,-1, 5][ -4, 0, 4][ -4, 1, 3][ -4, 2, 2][ -3,-2, 5][ -3,-1, 4][ -3, 0, 3][ -3, 1, 2][-2,-2, 4][-2,-1, 3][-2, 0, 2][-2, 1, 1][-1,-1, 2][-1, 0, 1][0, 0, 0]

The relevant code is placed in the personal GITHUB:HTTPS://GITHUB.COM/GANNYEE/LEETCODE/TREE/MASTER/SRC

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. Personal GitHub code space: Https://github.com/gannyee

Leetcode Problem Solving report--3 Sum

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.