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.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
###method 1:
class Solution(object):
def twoSum(self, nums, target):
a=0
for i in nums:
# print 'i',i
b=0
for j in nums:
# print 'j',j
if i + j == target:
if a<b:
return [a,b]
b +=1
a += 1
###method 2:
class Solution(object):
def twoSum(self, nums, target):
i = 1
while i <= len(nums):
j=0
while j < i:
sums = nums[i] + nums[j]
if sums == target:
if i > j:
return [j,i]
else:
return [i,j]
j += 1
i += 1