题目
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
class Solution(object): def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ a1 = 0 a2 = 0 for i in range(len(nums)): for j in range((i+1),len(nums)): if target - nums[i] == nums[j]: a1 = i a2 = j break if a2 != 0: break return (a1,a2)

本文介绍了一个经典的算法问题“两数之和”的解决方案。给定一个整数数组和一个目标值,找出数组中和为目标值的两个数,并返回它们的数组下标。文章通过双层循环的方式实现了这一功能。
2399

被折叠的 条评论
为什么被折叠?



