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].

Difficulty: introductory

Starter code:

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        

Solutions:
class Solution:
     def twoSum(self, nums, target):
         tmp = {}
         for i in range(len(nums)):
             if target - nums[i] in tmp:
                 return(tmp[target - nums[i]], i)
             else:
                 tmp[nums[i]] = i;
         
         """
         :type nums: List[int]
         :type target: int
         :rtype: List[int]
         """


class Solution:
     def twoSum(self, nums, target):
         """
         :type nums: List[int]
         :type target: int
         :rtype: List[int]
         """
         d = {}
         for i, num in enumerate(nums):
             if (target - num) in d:
                 return [d[target-num], i]
             d[num] = i
         
         new = sorted(nums)
         i,j = 0, -1
         for num in new:
             a, b = new[i], new[j]
             if a + b > target:
                 j = j - 1
             elif a + b < target:
                 i = i + 1
             elif a + b == target:
                 if a != b:
                     ans = [nums.index(a), nums.index(b)]
                 else:
                     m = nums.index(a)
                     nums.remove(a)
                     n = nums.index(b)
                     ans =[m, n+1]
                 return (ans)
         
 


class Solution(object):
     def twoSum(self, nums, target):
         hashdict = {}
         for i,num in enumerate(nums):
             if target-num in hashdict:
                 return [hashdict[target-num], i]
             else:
                 hashdict[num] = i


# Using 'dict', like hash, to find the pair number
 class Solution:
     def twoSum(self, numbers, target):
         ans = []
         dir = {}
         ll = len(numbers)
         for i in range(ll):
             dir[numbers[i]] = i
         for i in range(ll):
             o2 = target-numbers[i]
             if o2 in dir:
                 if (dir[o2] != i):
                     ans.append(i)
                     ans.append(dir[o2])
                     return ans

class Solution:
     def twoSum(self, nums, target):
         '''
         type nums:array
         type target:integer
         rtype :List
         '''
         d = {}
         for i, num in enumerate(nums):
             if target - num in d:
                 return[d[target - num], i]
             d[num] = i    

# Using 'dict', like hash, to find the pair number
 class Solution:
     def twoSum(self, numbers, target):
         ans = []
         dir = {}
         ll = len(numbers)
         for i in range(ll):
             dir[numbers[i]] = i
         for i in range(ll):
             o2 = target-numbers[i]
             if o2 in dir:
                 if (dir[o2] != i):
                     ans.append(i)
                     ans.append(dir[o2])
                     return ans

class Solution:
     def twoSum(self, nums, target):
         """
         :type nums: List[int]
         :type target: int
         :rtype: List[int]
         """
         t=dict()
         l=len(nums)
         if l<=1: return []
         for i in range(l):
             t[nums[i]]=i
         for i in range(l):
             comp=target-nums[i]
             if comp in t and t[comp]!=i:
                 return [i,t[comp]]
         return []

class Solution(object):  
     def twoSum(self, nums, target):  
         """ 
         :type nums: List[int] 
         :type target: int 
         :rtype: List[int] 
         """  
         arr = {}
         length = len(nums)
         for i in range(length):  
             if (target - nums[i]) in arr:
                 return [arr[target - nums[i]], i]
             arr[nums[i]] = i

class Solution:
         
     def twoSum(self, nums, target):
         """
         :type nums: List[int]
         :type target: int
         :rtype: List[int]
         """
         hashmap = {}
         x = len(nums)
         for i in range(x):
             comp = target - nums[i]
             if comp in hashmap:
                 return [hashmap.get(comp), i]
             hashmap[nums[i]] = i
             


class Solution:
     def twoSum(self, nums, target):
         """
         :type nums: List[int]
         :type target: int
         :rtype: List[int]
         """
         sorted_nums = sorted(nums)
         start = 0
         end = len(nums) - 1
         while start < end:
             curr_sum = sorted_nums[start] + sorted_nums[end]
             if (curr_sum == target):
                 break
             if (curr_sum < target):
                 start += 1
             else:
                 end -= 1
         first_index = nums.index(sorted_nums[start])
         second_index = nums.index(sorted_nums[end])
         if sorted_nums[start] == sorted_nums[end]:
             nums.pop(first_index)
             second_index = nums.index(sorted_nums[end]) + 1
         return [first_index, second_index]


class Solution:
     def twoSum(self, nums, target):
         """
         :type nums: List[int]
         :type target: int
         :rtype: List[int]
         """
         table = {}
         for i, e in enumerate(nums):
             print("fe")
             if e in table:
                 table[e] = [table[e][0] + 1, table[e][1] + [i]]
             else:
                 table[e] = [1, [i]]
             
             sub_target = target - e
             if sub_target in table and (sub_target != e or table[sub_target][0] >= 2):
                 print(table)
                 first_idx = i
                 second_idx = None
                 for index in table[sub_target][1]:
                     if index != first_idx:
                         second_idx = index
                         break
                 
                 assert(second_idx, "got shit")
                 result = [first_idx, second_idx]
                 result.sort()
                 return result
         return []

class Solution(object):
     def twoSum(self, nums, target):
         hashdict = {}
         for i,num in enumerate(nums):
             if target-num in hashdict:
                 return [hashdict[target-num], i]
             else:
                 hashdict[num] = i


class Solution(object):
     def twoSum(self, nums, target):
         nums_index = [(v, index) for index, v in enumerate(nums)]
         nums_index.sort()
         begin, end = 0, len(nums) - 1
         while begin < end:
             curr = nums_index[begin][0] + nums_index[end][0]
             if curr == target:
                 return [nums_index[begin][1], nums_index[end][1]]
             elif curr < target:
                 begin += 1
             else:
                 end -= 1


class Solution:
     def twoSum(self, nums, target):
         """
         :type nums: List[int]
         :type target: int
         :rtype: List[int]
         """
         numsMap = {}
         for i, num in enumerate(nums):
             if target - num in numsMap:
                 return [i, numsMap[target-num]]
             numsMap[num] = i
         return [None, None]

class Solution:
     def twoSum(self, nums, target):
         """
         :type nums: List[int]
         :type target: int
         :rtype: List[int]
         """
 #         length_nums = len(nums)
         
 #         for i in range(length_nums):
 #             for j in range(i+1, length_nums):
 #                 if nums[i] + nums[j] == target:
 #                     return [i, j]
 
         # dic = {}
         # for i, num in enumerate(nums):
         #     if num in dic:
         #         return [dic[num], i]
         #     else:
         #         dic[target - num] = i
         
         num_dict = {}
         for i, num in enumerate(nums):
             rem = target - num
             if rem in num_dict:
                 return [num_dict[rem], i]
             num_dict[num] = i
         return None

class Solution:
     def twoSum(self, nums, target):
         """
         :type nums: List[int]
         :type target: int
         :rtype: List[int]
         """
         dict = {}
         for i in range(0,len(nums)):
             if nums[i] in dict:
                 return [dict[nums[i]],i]
             else:
                 dict[target-nums[i]] = i
         return []

class Solution:
     def twoSum(self, nums, target):
         """
         :type nums: List[int]
         :type target: int
         :rtype: List[int]
         """
         indexes = dict([(nums[i], i) for i in range(len(nums))])
         for i in range(len(nums)):
             if target-nums[i] in indexes and indexes[target - nums[i]] != i:
                 return [i, indexes[target - nums[i]]]
         return []

class Solution:
     def twoSum(self, nums, target):
         """
         :type nums: List[int]
         :type target: int
         :rtype: List[int]
         """
         
         # record the sorted index (original position)
         nums_sorted_index = sorted(range(len(nums)), key = lambda k: nums[k])
         # sort the list
         nums.sort()
         for i in range(len(nums)):
             for j in range(i+1,len(nums)):
                 if nums[i]+nums[j] > target:
                     break
                 elif nums[i]+nums[j] == target:
                     return [nums_sorted_index[i], nums_sorted_index[j]]
         print('Can\'t find a match!')

class Solution:
     def twoSum(self, nums, target):
         
         """
         dic={}
         for i,now in enumerate(nums):
             dev = target - now
             if dev in dic:
                 return [dic[dev],i]
             dic[now]=i
         return None
         """
     
         
     #   old
         a=sorted(nums)
         
         for j in range(len(nums)):
             for k in range(j+1,len(nums)):
                 s = a[j]+a[k]
                 if s == target:
                     if a[j]==a[k]:
                         return [i for i,x in enumerate(nums) if x == a[k]]
                     else:
                         b=nums.index(a[j])
                         c=nums.index(a[k])
                         return [b,c]
                         
                 elif s>target:
                     break
                
         
         """
         :type nums: List[int]
         :type target: int
         :rtype: List[int]
         """


# Using 'dict', like hash, to find the pair number
 class Solution:
     def twoSum(self, numbers, target):
         ans = []
         dir = {}
         ll = len(numbers)
         for i in range(ll):
             dir[numbers[i]] = i
         for i in range(ll):
             o2 = target-numbers[i]
             if o2 in dir:
                 if (dir[o2] != i):
                     ans.append(i)
                     ans.append(dir[o2])
                     return ans