python-leetcode1 find the two numbers is equal to a target value in an integer array, and returns the list there are two subscripts

# Given an array of integers and a target, find two numbers in the array and to the target value. 
# You can assume only one answer corresponding to each input, and the same element can not be reused.
Example #: = Given the nums [2,. 7,. 11, 15], target =. 9
# because nums [0] + nums [1 ] = 2 + 7 = 9 is returned [0, 1]
class Solution():
    def twosum(self,nums,target):
         nums_index=[]
        for i in range(len(nums)):
            for j in range(i+1,len(nums)):
                if nums[i]+nums[j]==target:
                    nums_index.append(i)
                    nums_index.append(j)
        return nums_index

if __name__=="__main__":
        nums=[2,7,8,9,11]
        target=9
        l=Solution()
        res=l.twosum()
        print(res)





Arch day a soldier.

Guess you like

Origin www.cnblogs.com/qfdmmh/p/10906779.html