알고리즘 풀이/LeetCode

[LeetCode] 1. Two Sum

12.tka 2022. 6. 12. 14:20
728x90

문제 보기

[사용한 알고리즘]

x

 

[설명]

nums 리스트에 있는 원소 중 두 원소의 합이 target 이 되는 경우는 1가지가 존재한다. 따라서, 2중 반복문을 수행하면 쉽게 두 원소를 찾을 수 있다.

 

[코드]

class Solution:
    def twoSum(self, nums: List[int], target: int) -> 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]
728x90