[Leetcode] [015] 3Sum (Java)

Source: Internet
Author: User

The title is here: https://leetcode.com/problems/3sum/

"Label" Array; Pointers

"Personal Analysis"

Frankly, this problem card half-day, the first time do not, copy others. After a long time, the second time, or not ... Several times are time Limited Error. After reading the correct answer, I know it is using the optimization of the Pointers + sort.

How to optimize? Simply put, it is by sorting + skipping repetition (using the pointers) to avoid duplicates in the topic.

Core idea: We give the final 3 number of decimal group called triplet.

1. Lock the first number in the triplet first to index I (line 12),

2. Then we want to find two numbers from [Nums[i + 1], Nums[end]] and make two numbers (0-nums[i])

Key steps: 1. When the first digit index is locked, all duplicated digits are skipped (line 16). Until we find the next non-repeating number as the first number in the triplet.

2. When looking for two numbers from [Nums[i + 1], nums[end], use the left pointer and right pointer. If left, right two numbers and Twosumtarget (line 39)

Small words, only by moving the left pointer to the right, so as to leave a greater and greater, and thus closer to the twosumtarget.

"A Little Experience"

In the O (n^2) algorithm, consider adding O (NLGN) to the sort operation. In particular, when the need to remove duplication, the use of sorting + double pointers to achieve the hashset effect.

1  Public classSolution {2      PublicList<list<integer>> Threesum (int[] nums) {3list<list<integer>> result =NewArraylist<list<integer>>();4         intLen =nums.length;5         if(Len < 3) {6             returnresult;7         }8         9 Arrays.sort (nums);Ten          One         //For all number so can be the 1st number of triplet A          for(inti = 0; i < len-1; i++) { -             intFirstnumber =Nums[i]; -              the             //Skip all duplicated first number -             if(i = = 0 | | Firstnumber! = nums[i-1]) { -                  -                 intLeftindex = i + 1; +                 intRightindex = len-1; -                 intTwosumtarget = 0-Firstnumber; +                  A                 //try to find, numbers, sum to Twosumtarget at                  while(Leftindex <Rightindex) { -                     intTwosum = Nums[leftindex] +Nums[rightindex]; -                     if(Twosum = =twosumtarget) { -                         //One valid triplet found!! - Result.add (Arrays.aslist (Firstnumber, Nums[leftindex], Nums[rightindex])); -                         //Skip duplicated Nums[leftindex] in                          while(Leftindex < Rightindex && Nums[leftindex] = = Nums[leftindex + 1]) { -leftindex++; to                         } +                         //Skip duplicated Nums[rightindex] -                          while(Leftindex < Rightindex && Nums[rightindex] = = Nums[rightindex-1]) { therightindex--; *                         } $                         //move to next non-duplicatesPanax Notoginsengleftindex++; -rightindex--; the}Else if(Twosum <twosumtarget) { +                         //move left towards right to A                         //Make twosum larger to get closer to Twosumtarget theleftindex++; +}Else { -rightindex--; $                     } $                 } -                  -             } the         } -         Wuyi         returnresult; the     } -}

[Leetcode] [015] 3Sum (Java)

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.