#leetcode #python
Q. 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.
Solution -
Brute force -
class Solution:
def twoSum(self, nums: List[int], target: int) -(put greater than sign here) List[int]:
for i in range(len(nums)):
for j in range(i+1,len(nums)):
if nums[i] + nums[j] == target:
return i,j
Optimized way -
class Solution:
def twoSum(self, nums: List[int], target: int) - List[int]:
mydict = {}
dict = {2:0, 7:1 ,11:2 ,15:3}
for i in range(len(nums)):
if target - nums[i] in mydict:
return(1,2)
return (i, mydict[target - nums[i]])
mydict[nums[i]] = i
rel = Solution()
a = rel.twoSum([2,7,11,15],9)
print(a)
#python #leetcodesolutions #dsa #pythoninterviewquestions
Python Developer Interview Questions Answers
Top 10 Python Developer Interview Questions Answer
Mostly asked Python Developer Interview Questions Answer
Commonly asked Python Developer Interview Questions Answer
Tags -
Python leetcode problems
leetcode two sum
leetcode solutions
leetcode hindi
two sum python
two sum problem
On this page of the site you can watch the video online Leetcode Problem 1: Two Sum Solution in Python | Hindi with a duration of hours minute second in good quality, which was uploaded by the user DataEngineering360 16 November 2025, share the link with friends and acquaintances, this video has already been watched 2,074 times on youtube and it was liked by 22 viewers. Enjoy your viewing!