[Leetcode] (python): 015-3sum

Source: Internet
Author: User

Source of the topic:

https://leetcode.com/problems/3sum/

Test Instructions Analysis:

This topic is input an array of nums. Find out all 3 numbers so that the sum of the 3 numbers is 0.1. The 3 digits of the output are sorted by small to large, and the combination of 2.3 numbers is not repeated. For example, enter [ -1,0,1,2,-1,-4], which should return [[ -1,0,1],[-1,-1,2]].

Topic Ideas:

If the direct violence is resolved, then the time complexity is (O (n^3)). This will be tle.

Seeing this topic, I recalled the first question of Leetcode. The first question is to give an array and a target to find out the two numbers of the array so that the two numbers equals target. In the first question, I mentioned the "clamping theorem". This theorem can be used here. First , we'll sort the input array nums. then , from the beginning, take a number, nums[i], in nums[i+1:] with the clamping theorem to find Num[j],nums[k] (j<k) make their and for 0-nums[i]. Then [Nums[i],nums[j],nums[k]] Append to the answer array. Because there will be multiple combinations so nums[i] + nums[j] + nums[k] = 0, so at the time of comparison, if NUMS[J] + Nums[k] < 0-nums[i] time, j + = 1; if NUMS[J] + nums[k] > 0-nums[i] time, k-= 1; if NUMS[J] + nums[k] = = 0-nums[i], always j + = 1,k-= 1 until nums[j]! = Nums[j-1] and nums[k]! = nums[k + 1]. Note that in order to avoid duplicate combinations, i + should always be added to nums[i]! = nums[i-1].

In this method, the time complexity of sorting is (O (N*log (n))), the time complexity of the clamping theorem (o (n)), the complexity of the FETCH (O (n)), the total time complexity (O (N*log (n)) + O (n) *o (n)) = O (n^2). so the complexity of Time is O (n^2).

Code (Python):

1 classsolution (object):2     defthreesum (Self, nums):3         """4 : Type Nums:list[int]5 : Rtype:list[list[int]]6         """7Size =Len (nums)8Ans = []9         ifSize <= 2:Ten             returnans One Nums.sort () Ai =0 -          whileI < size-2: -TMP = 0-Nums[i] thej = i + 1 -K = size-1 -              whileJ <K: -                 ifNUMS[J] + Nums[k] <tmp: +J + = 1 -                 elifNUMS[J] + nums[k] >tmp: +K-= 1 A                 Else: at ans.append ([nums[i],nums[j],nums[k]]) -J + = 1 -K-= 1 -                      whileJ <K: -                         ifNUMS[J]! = nums[j-1]: -                              Break in                         ifNUMS[K]! = nums[k + 1]: -                              Break toJ + = 1 +K-= 1 -i + = 1 the              whileI < size-2: *                 ifNums[i]! = nums[i-1]: $                      BreakPanax Notoginsengi + = 1 -         returnAns
View Code

Reprint Please specify source: http://www.cnblogs.com/chruny/p/4820473.html

[Leetcode] (python): 015-3sum

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.