Algorithm (PS)

[LeetCode] Two Sum Python

minjiwoo 2023. 4. 24. 22:44
728x90

https://leetcode.com/problems/two-sum/submissions/938964500/

 

Two Sum - LeetCode

Can you solve this real interview question? Two Sum - 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

leetcode.com

 

유형 : Brute Force

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        answer = []
        # index 를 담아서 리턴한다
        N = len(nums)
        for i in range(N-1):
            for j in range(i+1, N):
                if nums[i] + nums[j] == target:
                    answer.append(i)
                    answer.append(j)
        return answer
728x90