Author: 18th Cui Yunlong
Date: 2020-10-3
Tags: Simple Traversal
Title description
Given an integer array nums and a target value target, please find the two integers whose sum is the target value in the array and return their array subscripts.
You can assume that each input will only correspond to one answer. However, the same element in the array cannot be used twice.
Example:
Given nums = [2, 7, 11, 15], target = 9
Because nums[0] + nums[1] = 2 + 7 = 9,
it returns [0, 1]
Problem-solving ideas:
Iterate over
Code
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
for i in range(len(nums)):
a = nums[i]
b = target-a
for j in range(len(nums)-1,i,-1):
if nums[j] == b:
return [i,j]