Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
我的代码:
def two_sum(arr_list, target):
result = []
length = len(arr_list)
for i in range(length - 1):
for j in range(i + 1, length):
if arr_list[i] + arr_list[j] == target:
result.append(i)
result.append(j)
print(result)
if __name__ == "__main__":
arr = [3, 4, 5, 6]
target = 9
print("target type is :", type(target))
two_sum(arr, target)
print("ok")