leetcode 1 Two Sum

1 Two Sum     37.00% 查找数组中相加等于给定数字的两个元素
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 24 13:56:44 2017

@author: vicky
"""

#1. Two Sum 

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

#class Solution:
#    def twoSum(self, nums, target):
#        """
#        :type nums: List[int]
#        :type target: int
#        :rtype: List[int]
#        """
#        b={} #指针
#        for i in range(len(nums)):
#            b[nums[i]]=i
#            print(b)
#        #for i in range(len(nums)):
#            if (target-nums[i] in b):# and i

 

你可能感兴趣的:(刷题)