Two Sum

1 min read
Given an array of integers nums
and an integer target
, return indices of the two numbers such that they add up to target
.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
for i in range(len(nums)):
for j in range(i+1, len(nums)):
if target-nums[i] == nums[j]:
return [i, j]
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
maps=[]
for i in range(len(nums)):
maps.append(target-nums[i])
for i in range(len(nums)):
if nums[i] in maps and i is not maps.index(nums[i]):
return [i, maps.index(nums[i])]
0
Subscribe to my newsletter
Read articles from khushaalan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
