"Leetcode with Java" 4Sum (on^2)

Source: Internet
Author: User

I saw a few popular high blog, they do not do this algorithm O (n^2), so I put the solution in advance to share, for your reference (the topic of the previous skip will be made up in the near future).


"title":

Given an array S of n integers, is there elements a, B, C, and D in S such that A + B + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note:elements in a quadruplet (a,b,c,d) must is in non-descending order. (ie, a≤b≤c≤d) The solution set must not contain duplicate quadruplets.    For example, given array S = {1 0-1 0-2 2}, and target = 0.    A solution set is:    ( -1,  0, 0, 1)    ( -2,-1, 1, 2)    ( -2,  0, 0, 2)


"On the Code"

Import Java.util.arraylist;import java.util.collections;import java.util.enumeration;import java.util.Hashtable; Import java.util.list;import java.util.vector;/* * Reference: No reference to others algorithm * O (n^2) * idea: first to plug each logarithm into a hashtable (with this logarithm and key, Value as a set containing all and key pairs). * Then traverse the Hashtable, for each set of pairs of keys, the set of get and number pairs for target-key in that Hashtable, * if get to is empty, then continue, if get to is not empty, Then set these two pairs and then an ordered 4-tuple, and see if the previous * 4-tuple has already appeared (using another hastable<string,anytype>:reappear to quickly determine the reappear's * Key is a concatenation of 4 integers with an ordered 4-tuple). If the 4-tuple is present, it is added to the result set. * */public class Solution1 {public list<list<integer>> foursum (int[] num, int target) {List<list<integ er>> list = new arraylist<list<integer>> (); if (num = = NULL | | Num.length < 4) return List;int len = nu M.length; hashtable<integer,pairrecoder> prs = new hashtable<integer,pairrecoder> (Len*len); for (int i = 0; i < len- 1; i++) {for (int j = i + 1; j < Len; J + +) {int sum = Num[i]+num[j]; Pairrecoder PR = prs.get (num[i]+num[j]); if (pr = = null) prs.put (Sum, new Pairrecoder (SUM,I,J)), Elsepr.pairs.add (New Pair (I,J));}} List<four> fours = new arraylist<four> (); hashtable<string,boolean> reappear = new hashtable<string,boolean> (); For (enumeration<pairrecoder> e=prs.elements (); e.hasmoreelements ();) {Pairrecoder PR1 = (pairrecoder) e.nextelement (); Pairrecoder PR2 = Prs.get (TARGET-PR1.V), if (PR2 = = null) continue;for (int i=0;i<pr1.pairs.size (); i++) for (int j=0;j <pr2.pairs.size (); j + +) {Pair P1 = pr1.pairs.elementAt (i); Pair P2 = pr2.pairs.elementAt (j); if (p1.v1!=p2.v1 &&p1.v1!=p2.v2 && p1.v2!=p2.v1 && p1.v2!= P2.V2) {Four of = new Orderedfour (Num[p1.v1],num[p1.v2],num[p2.v1],num[p2.v2]); StringBuffer sb = new StringBuffer (), Sb.append (of.num.get (0)), Sb.append (Of.num.get (1)), Sb.append (Of.num.get (2)); Sb.append (Of.num.get (3)), if (!reappear.containskey (sb.tostring ())) {List.add (of.num); System.out.println ("NEW:" +SB); Reappear.put (Sb.tostring (), True);}}} return list;} Class Pair {int V1;int v2; Pair (intv1, int v2) {this.v1 = V1;this.v2 = v2;}} Class Pairrecoder{int V; vector<pair> pairs = new vector<pair> (); Pairrecoder (int v,int i,int j) {this.v = V;this.pairs.add (new Pair (I,J));}} Class four{list<integer> num; Four (int v1,int v2,int v3,int v4) {this.num = new arraylist<integer> (); Num.add (v1); Num.add (v2); Num.add (v3); Num.add (v4);}} Class Orderedfour extends Four{orderedfour (int v1,int v2,int V3,int v4) {Super (V1, V2, v3, v4); Collections.sort (This.num);}}}




"Leetcode with Java" 4Sum (on^2)

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.