# 3Sum time Limit exceeded HASHMAP optimization process

Source: Internet
Author: User

Last night, in doing leetcode on the 3Sum topic, feel this problem and 2Sum very much like, at that time to solve 2Sum, the idea is as follows:

With HashMap key storage num[i],value storage subscript I, and then when traversing the array num, Judge Target-num[i] whether in the HashMap key, the approximate problem-solving idea is this. So I decided to continue using this idea to solve the 3Sum problem. Ideas are as follows:

With a 2-layer for loop (the same 4Sum problem with a 3-layer for loop), the Java code (this code time Limit exceeded) is as follows:

` PublicList<list<integer>> Threesum (int[] num)        {arrays.sort (num); List<List<Integer>> Listall =NewArraylist<list<integer>>(); Map<integer, integer> HM =NewHashmap<integer, integer>();  for(inti = 0; i < num.length; i++) {hm.put (num[i], i); }         for(inti = 0; i < num.length; i++) {            intONETMP =-Num[i]; if(ONETMP < 0) {                    Break; }             for(intj = i + 1; J < Num.length; J + +) {                intThirdtmp = ONETMP-Num[j]; if(Hm.containskey (thirdtmp) && hm.get (thirdtmp) >j) {List<Integer> listtmp =NewArraylist<integer>();                    Listtmp.add (Num[i]);                    Listtmp.add (Num[j]); Listtmp.add (THIRDTMP);                     if (! listall.contains (listtmp)) {listall.add (listtmp); }                }            }        }                                                    returnListall; }`

The time Limit exceeded is always displayed when the above code is submitted. After inspection, found that the problem appears in the red code above, I calculated a lot of repeated triplet, and then use Listall contains to judge and go to heavy, such operations will take a certain amount of time, resulting in Exceeded.

First optimization: optimize the Traversal method of subscript J for the second layer for loop , Java code (oddly enough, this code is accepted the first time it was submitted, and then several commits are time Limit exceeded) as follows:

` PublicList<list<integer>> Threesum (int[] num)        {arrays.sort (num); List<List<Integer>> Listall =NewArraylist<list<integer>>(); Map<integer, integer> HM =NewHashmap<integer, integer>();  for(inti = 0; i < num.length; i++) {hm.put (num[i], i); }         for(inti = 0; i < num.length; i++) {            intONETMP =-Num[i]; if(ONETMP < 0) {                    Break; }             for(intj = i + 1; J < Num.length; J + +) {                intThirdtmp = ONETMP-Num[j]; if(Hm.containskey (thirdtmp) && hm.get (thirdtmp) >j) {List<Integer> listtmp =NewArraylist<integer>();                    Listtmp.add (Num[i]);                    Listtmp.add (Num[j]);                                        Listtmp.add (THIRDTMP);                    Listall.add (listtmp); While   ((J < num.length-1) && (num[j] = = num[j + 1 ])) {J                         ++; }                }            }        }                            returnListall; }`

In fact, the Red part of the first piece of code is replaced with a yellow part.

The second optimization, is to optimize the first layer for loop subscript I traversal method, the final Java code (this time with 318ms accepted) as follows:

` PublicList<list<integer>> Threesum (int[] num)        {arrays.sort (num); List<List<Integer>> Listall =NewArraylist<list<integer>>(); Map<integer, integer> HM =NewHashmap<integer, integer>();  for(inti = 0; i < num.length; i++) {hm.put (num[i], i); }         for(inti = 0; i < num.length; i++) {            intONETMP =-Num[i]; if(ONETMP < 0) {                    Break; }             for(intj = i + 1; J < Num.length; J + +) {                intThirdtmp = ONETMP-Num[j]; if(Hm.containskey (thirdtmp) && hm.get (thirdtmp) >j) {List<Integer> listtmp =NewArraylist<integer>();                    Listtmp.add (Num[i]);                    Listtmp.add (Num[j]); Listtmp.add (THIRDTMP);Listall.add (LISTTMP);  while(J < Num.length-1) && (num[j] = = num[j + 1])) {J++; }                }            }            While   ((i < num.length-1) && (num[i] = = num[i + 1 ])) {i                 ++; }        }                    returnListall; }`

In fact, on the basis of the second piece of code, plus the green code of the face . at this point, the Java code optimization for solving 3Sum problems based on HashMap ends ~

3Sum time Limit exceeded HASHMAP optimization process

Related Keywords:

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.