python刷leetcode

第一题:给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。

示例:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]

class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """      

        #result = []
        for i in range(len(nums)):
            for j in range(i+1,len(nums)):
                sum = nums[i] + nums[j]
                if sum == target:
                    #result.append(i)
                    #result.append(j)
                    return (i,j)

初级水平代码,运行4000ms

posted @ 2018-10-20 11:25  迷迷糊糊的礼物  阅读(457)  评论(0编辑  收藏  举报