1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| def two_sum(nums, target): for i in range(len(nums)): for j in range(i+1, len(nums)): if nums[j] == target - nums[i]: return [i, j]
nums = [2, 7, 8, 9] target = 9 result = two_sum(nums, target) print(result)
class Solution: def two_sum(self, nums, target): for i in range(len(nums)): for j in range(i+1, len(nums)): if nums[j] == target - nums[i]: return [i, j]
solution = Solution()
nums = [2, 7, 8, 9] target = 9 result = solution.two_sum(nums, target) print(result)
|