[Face question] Find 3 numbers in the array so that they are 0

Source: Internet
Author: User

Given an array of s, try to find 3 numbers A, B, C, making a+b+c=0. That is to find out all the 0 and 3 numbers from the collection. For example: Set s={-1,0, 1, 2,-1, 4}, the 3-digit number that satisfies the condition has 2 pairs: (-1, 0, 1) and ( -1, 2,-1)。 Note ( -1,1,0) that the same solution is calculated with ( -1,0,1), so there is no need to think again. The solution of this example collection can also be written as: (0, 1,-1) and (2,-1,-1)。 Reference: http://blog.csdn.net/wangran51/article/details/8858398, he gave the code a little bit of a problem. overriding algorithms using Java code
1  Packagecom.sum3;2 3 Importjava.util.ArrayList;4 Importjava.util.Collections;5 ImportJava.util.HashSet;6 Importjava.util.List;7 ImportJava.util.Set;8 9  Public classSum3 {Ten  One     Private StaticSet<list<integer>> fing_sum3 (list<integer>arr) { ACollections.sort (arr);//sort an array first -          for(Integer ainteger:arr) { - System.out.println (Ainteger); the         } -List<integer>List3; -Set<list<integer>> setlist =NewHashset<>(); -          +          for(inti = 0; I < Arr.size ()-2; i++){ -             intj = i+1; +             intK = Arr.size ()-1; A              while(J <k) { at                 //fixed arr[i] does not move, the left and right approximation; arr[j] too small, move forward one -                 if(Arr.get (i) + Arr.get (j) + Arr.get (k) < 0 ) { -J + +; -}Else if(Arr.get (i) + Arr.get (j) + Arr.get (k) > 0){ -k--; -}Else { inList3 =NewArraylist<>(); - List3.add (Arr.get (i)); to List3.add (Arr.get (j)); + List3.add (Arr.get (k)); - Setlist.add (LIST3); the                     //j++;//This needs to be erased, otherwise some elements are not counted. *k--; $                 }Panax Notoginseng             } -         } the         returnsetlist; +     } A      the      +      Public Static voidMain (string[] args) { -Integer[] List = {2,1,0,-1,1,4,2,-2,-3}; $Arraylist<integer> ArrayList =NewArraylist<>(list.length); $ Collections.addall (arrayList, list); -set<list<integer>> output =fing_sum3 (arrayList); -          for(list<integer>list2:output) { the System.out.println (list2); -         }Wuyi     } the  -}

Output Result:

-3-2-1 0 1 1 2 2 4


[-3,-1, 4]
[-3, 1, 2]
[-2, 1, 1]
[-2, 0, 2]
[-1, 0, 1]

[Face question] Find 3 numbers in the array so that they are 0

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.