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: EASY
Tags: Data structures
Skill types: Data structures

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


class Solution:

	def twoSum(self, nums, target):
		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


class Solution:

	def twoSum(self, nums, target):
		d = {}
		for (i, num) in enumerate(nums):
			if target - num in d:
				return [d[target - num], i]
			d[num] = i


class Solution:

	def twoSum(self, nums, target):
		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):
		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):
		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):
		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):
		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):
		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):
		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):
		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):
		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):
		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):
		nums_sorted_index = sorted(range(len(nums)), key=lambda k: nums[k])
		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):
		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