[Power button - small daily practice] 1. The sum of two numbers (python)

1. The sum of two numbers

Title Description

Given an array of integers numsand a target value target, and you figure out a 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.

Examples

Given nums = [2, 7, 11 , 15], target = 9
because nums [0] + nums [1 ] = 2 + 7 = 9
is returned [0, 1]

Code implements (Python)

class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        n = len(nums)
        for i in range(n):
            for j in range(i+1,n):
                if nums[i]+nums[j] == target:
                    return [i,j]
Published 44 original articles · won praise 5 · Views 4473

Guess you like

Origin blog.csdn.net/ljb0077/article/details/104700527