[Leetcode] [15]3sum analysis and fast sorting algorithm-java implementation

Source: Internet
Author: User

Q:

Given an arrayS ofNintegers, is there elementsa, b, CinchSsuch thata + 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, 0, 1, 2,-1, -4],a solution set is:[  [-1, 0, 1],  [-1,-1, 2]]
A:

I started to look at the problem yesterday, but I don't have a good idea, the way to think about it is to divide and cure something similar to getting a number nums[0], and then find out her inverse number. Then I went to check the information, there is a solution to the ksum of the topic, it seems that this problem is difficult to have a good solution, should be the best O (n^ (k-1)) complexity. So follow yesterday's idea to do so.

1, solve such problems for speed must be sorted first.

2, take the x number x from 0 to N-1

3. Compare the remaining nums[i+1]nums[n-1] (x=i);

4, if nums[i+1]+nums[n-1]>target, then can only n--can ==target

if nums[i+1]+nums[n-1]<target, then you can only i++ ==target

5, if I==n or nums[i+1]+nums[n-1]==target launch cycle, x + +;

In this case the general ranking is the worst O (n^2) and the latter is the worst sort O (n^2) so the overall algorithm O (n^2) can be guaranteed.

Let's look at the code below:

public static void Main (string[] args) {//system.out.println (); int[] Nums = { -5,1,-3,-1,-4,-2,4,-1,-1};//nums = Paixu ( NUMS,0,NUMS.LENGTH-1);//for (int i:nums) {//system.out.println (i);//}system.out.println (Method (Nums));} public static List<list<integer>> method (int[] nums) {list<list<integer>> list= new ArrayList <List<Integer>> (); if (nums.length<3) return list;nums = Paixu (nums,0,nums.length-1); for (int i:nums) { System.out.println (i);} Look for 3sumfor (int i = 0;i<nums.length;i++) {if (i==nums.length-1) return list;for (int j = i+1,k= (nums.length-1); j!=k;) { if (nums[j]+nums[k]> (-nums[i])) {k--;} else if (nums[j]+nums[k]< (-nums[i])) {j + +;} else if (nums[j]+nums[k]== (-nums[i])) {List numlist = new arraylist<integer> (); Numlist.add (Nums[i]); numlist.add (Nums[j]); Numlist.add (Nums[k]);//Go to heavy Boolean flag = True;for (List l:list) {if (l.tostring (). Equals (Numlist.tostring ()) ) {flag = False;break;}} if (flag) List.add (numlist); k--;}} return list;} public static int[] Paixu (iNt[] Nums,int Start,int end) {//Sort int key = Nums[start];int i = start,j=end;while (i<j) {while (i<j&&nums[j]& Gt;=key) {j--;} Nums[i] = Nums[j];while (i<j&&nums[i]<=key) {i++;} NUMS[J] = nums[i];} NUMS[J] = Key;//system.out.println ("Start:" +start+ "End" +end), if (start!=end) {//system.out.println ("J" +j); if (j== Start) Paixu (Nums, start+1, end), else if (j==end) Paixu (nums, Start, end-1), Else{paixu (nums,start,j-1);p Aixu (nums,j+1, end);}} return nums;}

Although the idea is not difficult, but boundary detection is really a headache ah.

It was later discovered that there was a long string of leetcode monitoring examples, but the time was too long, and then the code changed to this:

public static void Main (string[] args) {//system.out.println (); int[] Nums = {0,0,0};//nums = Paixu (nums,0,nums.length-1) ;//for (int i:nums) {//system.out.println (i);//}system.out.println (Method (Nums));} public static List<list<integer>> method (int[] nums) {list<list<integer>> list= new ArrayList <List<Integer>> (); if (nums.length<3) return list;//nums = Paixu (nums,0,nums.length-1); Arrays.sort (Nums); for (int i:nums) {System.out.println (i);} Find 3sumfor (int i = 0;i<nums.length-2;i++) {for (int j = i+1,k= (nums.length-1);j<k;) {if (Nums[j]+nums[k]> (-nums [i])) {k--;} else if (nums[j]+nums[k]< (-nums[i])) {j + +;} else{//if (nums[j]+nums[k]== (-nums[i])) List numlist = new arraylist<integer> (); Numlist.add (Nums[i]); Numlist.add (Nums[j]); Numlist.add (Nums[k]); System.out.println ("J:" +j);               while (J <k && nums[i] = = nums[i+1]&&i<nums.length-2) i++;  while (J < k && Nums[k] = = nums[k-1]) k--; List.add (numlist); j + +;}}}return list;} public static int[] Paixu (int[] nums,int Start,int end) {//Sort int key = Nums[start];int i = start,j=end;while (i<j) {while (I<j&&nums[j]>=key) {j--;} Nums[i] = Nums[j];while (i<j&&nums[i]<=key) {i++;} NUMS[J] = nums[i];} NUMS[J] = Key;//system.out.println ("Start:" +start+ "End" +end), if (start!=end) {//system.out.println ("J" +j); if (j== Start) Paixu (Nums, start+1, end), else if (j==end) Paixu (nums, Start, end-1), Else{paixu (nums,start,j-1);p Aixu (nums,j+1, end);}} return nums;}
The result is still not good ... Finally, the output from the method function can be removed.

[Leetcode] [15]3sum analysis and fast sorting algorithm-java implementation

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.