title requirements:
given nums = [2,7,11,15], target = 9
because nums [0] + nums [1] = 2 + 7 = 9
, return index [0,1]
topic Source: sum Sum on leetcode
personal code
def twoSum(nums, target):
l = [] -sharp
s = 0 -sharp
for _ in nums:
i = 0
s = s + 1
p = nums.pop(0)
l.append(p)
for v in nums:
if v == target - p:
return [len(l) - 1, s + i]
i = i + 1
personal thoughts and problems
my idea is to take an element into the external stack, record its value, and then traverse the elements that meet the criteria in the rest of the list.
s
, I
, in order to mark the sequence function of the elements that meet the requirements.
validation process found that every time I type
print(twoSum([1,3,3,5,2],7))
debugging results will show None
, that is, when both numbers are at the end of the input list , None
.
python novice, please give me more advice.