Leetcode (1) two numbers --- python

0, open a blog about the recording process of learning, that's all.

The first time you open the power button, do not know how to write code, how to perform, to tinker with for a long time, and finally know where to write. . . (Dish unparalleled either substance) are generally entitled to define a function, the function complements complete it. After we know that on the first pick of the simple, finished, execution, error, error correction, execution, error ,,, looked at other people's code, not read. . . Well, I started from the first question!

 

topic:

Given an integer array nums and a target value target, and ask you to identify the target value of the two integers in the array, and return to their array subscript.

You can assume that each input corresponds to only one answer. However, you can not re-use the same array element.

Example:

Given nums = [2, 7, 11, 15], target = 9

Because nums [0] + nums [1 ] = 2 + 7 = 9
is returned [0, 1]

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/two-sum
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

It seems not very difficult, twice traversal, returns the result of the addition of the establishment, a few steps finished. as follows:

class Solution:
  def twoSum(self, num: List[int], target: int) -> List[int]:
    for j in range(len(num)-1):
      for i in range(j+1,len(num)):
        if num[i] + num[j] == target:
          li = [j,i]
    return li

 

Time complexity: O (n²). With [2, 7, 11, 15], 9 test no problem, I'll submit go up. The results present the results show " beyond the time limit," a look at the list of test, a page does not fit. . .

Process I go online to search the answer suddenly thought of another way, immediately went to change the following code, as follows:

class Solution:
  def twoSum(self, num: List[int], target: int) -> List[int]:
    for j in range(len(num)-1):
      a = target - num[j]
      for i in range(j+1,len(num)):
        if num[i] == a:
          li = [j,i]
    return li

 

Time complexity: O (n²). The test is successful, submitted successfully!

 

 But it looks as if the efficiency of low points, the time complexity of a little big? Go online to search the following answers to the other, as follows

 

class Solution:
     DEF twoSum (Self, nums, target):
         "" " 
        : of the type nums: List [int] 
        : of the type target: int 
        : rtype: List [int] 
        " ""            
        the n- = len (nums)
         # Create an empty dictionary 
        d = {}
         for X in Range (n-): 
            A = target - nums [x]
             # presence nums [x] dictionary d in 
            IF nums [x] in d:
                 return d [the nums [X]], X
             # or to increase the dictionary key / value pairs 
            the else :
                D [A]= X
         # edge to increase the dictionary key / value pairs, side compared with nums [x]

Time complexity: O (1).

 

 I was too young to learn!

Guess you like

Origin www.cnblogs.com/tyt666/p/11440083.html