appium+python LeetCode 之旅 1:两数之和

三天打渔 · 2019年01月21日 · 732 次阅读

题目:
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。

测试用例:nums = [2, 7, 11, 15], target = 9
返回 [0, 1]

class Solution:
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        for i,n in enumerate(nums):
            if target-n in nums[i+1:]:
                return [i,nums[i+1:].index(target-n)+i+1]
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册