Hướng dẫn 2 sum leetcode python solution - 2 giải pháp python leetcode tổng hợp

num_lst = list(range(len(nums)))

for indx, num in enumerate(num_lst):

I'm not sure if I'm missing something, but I think not. I ran this code

nums = [2,5,7,9]
num_lst = list(range(len(nums)))
list(enumerate(num_lst))

output : [(0, 0), (1, 1), (2, 2), (3, 3)]

So why do you create the list and then enumerate it? Maybe what you want to do is simply : enumerate(nums) then enumerate(nums[index+1:]) on your other loop? A simpler way would be to only use the ranges, as I'll show below.

Also, given your input, there's a possibility that a single number would be higher than the target, in this case you shouldn't make the second iteration.

You also don't need the else: continue , as it's going to continue either way.

I'd end up with :

def twoSum(nums, target):
    """
    :type nums: List[int]
    :type target: int
    :rtype: List[int]
    """

    for i1 in range(len(nums)):
        if nums[i1] > target:
            continue

        for i2 in range(i1 + 1, len(nums)):
            if nums[i1] + nums[i2] == target:
                return [i1, i2]

    return None

Without knowing the expected input size, it's hard to focus on performance improvements. The main goal of my review was to remove what seemed like a misunderstanding in your code and in my opinion the code is clearer now.

Two Sum problem is considered easy and is frequently asked in Amazon phone interviews. We will see how we can start with a brute force solution and try to improve the code by introducing more data structures.

Simple Python Calculator (Video 25)

Please enable JavaScript

Problem Statement: Given an array of integers nums and an integer target return indices of the two numbers such that they add up to target

Our aim will be to reduce time complexity and space complexity.

The first solution that comes to mind is to go through the loop twice, each loop traverses the list, compares the sum of two numbers with the target, and returns the indices if the sum matches.

The time complexity of this algorithm is O(n^2). In this solution, we are not checking for null or multiple results because the question says, “the list returns one solution“.

class Solution:
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        length = len(nums)
        for i in range(length):
            for j in range(i + 1, length):
                if nums[i] + nums[j] == target:
                    return [i, j]

In our next solution, we can check whether the target-num[i] value exists in the rest of the list 

nums = [2,5,7,9]
num_lst = list(range(len(nums)))
list(enumerate(num_lst))

output : [(0, 0), (1, 1), (2, 2), (3, 3)]
0. This is a slightly better solution but still, the time complexity is not
nums = [2,5,7,9]
num_lst = list(range(len(nums)))
list(enumerate(num_lst))

output : [(0, 0), (1, 1), (2, 2), (3, 3)]
1.

class Solution:
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        length = len(nums)
        for i in range(length):
            diff = target - nums[i]         
            if diff in nums[i+1:]: 
                index = nums[i + 1:].index(diff)+i+1
                return [i, index]

Although the above method is faster than the first one, it is still not the best. Though it looks like we are looping once but in operator loops through the list to search for the diff variable.