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(name,target):
b_list = sorted(name)
flag = True
for i in range(len(b_list)):
for j in range(i+1,len(b_list)):
if (b_list[i]+b_list[j]) == target:
flag = False
print( "找到目標數,分別為({0},{1})".format(b_list[i],b_list[j]))
if flag:
print("沒有符合的項")
if __name__ == "__main__":
two_sum([1,3,5,2,4,6,100,130,150,160],12)