Both Sum | Leetcode OJ Problem Solving report

Source: Internet
Author: User

Topic website: https://oj.leetcode.com/problems/two-sum/

Title Description:

Given an array of integers, find the numbers such that they add up to a specific target number.

The function twosum should return indices of the numbers such that they add up to the target, where index1 must is Les S than Index2. Please note that your returned answers (both Index1 and INDEX2) is not zero-based.

You may assume this each input would has exactly one solution.

Input:numbers={2, 7, one, A, target=9

Output:index1=1, index2=2

The general meaning is to find two in a string of numbers plus equal to the target number of subscript (note subscript starting from 1), assuming only a set of solutions.

The first conclusion:

O(n2) Runtime, O(1) Space–brute Force:

The brute force approach are simple. Loop through each element x and the find if there is another value, the Equals to target–x. As finding another value requires looping through the rest of the array, its runtime complexity is O(n2).

Brute force, time complexity O(n2), this solution will time out (Run times Error)

O(n) Runtime, O(n) Space–hash table:

We could reduce the runtime complexity of looking up a value to O(1) using a hash map, maps a value to it in Dex.

Establish hash table lookup, time complexity O (n).

Here's an example I found on https://oj.leetcode.com/discuss/.

def twosum (self, num, target):    d = {} for    I in range (len (num)):        if num[i] in D:    # if D.has_key (Num[i]): 
   return (d[num[i]]+1, i+1)        else:            d[target-num[i]] = i                            

Suppose A_value+b_value=target defines a dictionary (b_value,a_index) key-value pair, traversing the number in the array. If B_value is in the dictionary, then output the subscript (B_index) of the number currently traversed and the value of this number in the dictionary a_index. If not, then create (Target-a_value,a_index)

See the difference between Has_key () and in

Http://stackoverflow.com/questions/1323410/has-key-or-in

Someone inside is going to be a little bit faster.

If you put the above code in the If Num[i] in D: Change to If Num[i] in D.keys (): Will cause a timeout, seemingly because the time to find the list is O (n), and the time to find Dict is O (1).

Not too shy paste oneself write code, didn't think with dict, in list inside toss to toss to the last tle

Both Sum | Leetcode OJ Problem Solving report

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.