标签:输入 get def == obj 整数 给定一个整数数组 style 循环
题目:
算法:
循环便利整数数组,找到小于等于target的数j,取到他的下标i,
然后从下表i+1开始便利取target-j,如果存在,则返回对应的下标
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
for i in range(len(nums)):
if nums[i] != target:
other_number = target - nums[i]
for j in range(i + 1, len(nums)):
if nums[j] == other_number:
print(i, j)
return [i, j]
if __name__ == "__main__":
nums = [-1, -2, -3, -4, -5]
target = -8
solution = Solution()
result = solution.twoSum(nums, target)
print(result)
标签:输入 get def == obj 整数 给定一个整数数组 style 循环
原文地址:https://www.cnblogs.com/syhshare/p/10605490.html