Leetcode Problem 1: Two Sum Solution in Python | Hindi

Опубликовано: 19 August 2026
на канале: DataEngineering360
2,074
22

#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