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?

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay