DEV Community

Cover image for Two sum - Leet Code Solution
Deepak Raj
Deepak Raj

Posted on • Edited on • Originally published at codeperfectplus.com

6 3

Two sum - Leet Code Solution

Two sum - Leet Code Solution

Topic: Two Sum Problem

Problem Statement:

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You can find the original question here -> Two Sum Problem

Two sum - Leet code solution in Python Language.

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

        for index, num in enumerate(nums):
            other = target - num 

            if other in  seen:
                return[seen[other],index]
            else:
                seen[num] = index
        return []
Enter fullscreen mode Exit fullscreen mode

Share Your Solutions?

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay