代码改变世界

LeetCode刷题笔记(二)

2018-03-19 19:02  cocolkk  阅读(160)  评论(0)    收藏  举报

1.Tow Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

第一次编写的代码,可以运行,点击Run Code以后,运行速度为30多ms,但是点击 Submit  Solution后,显示

class Solution:
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        list1 = []
        len_nums = len(nums)
        for i in range(len_nums):
            for j in range(len_nums):
                if i != j and nums[i] + nums[j] == target:
                    list1.append(i)
                    # list1.append(j)
        return list1