[LeetCode] Two Sum

김_리트리버·2020년 8월 12일
0

[알고리즘]

목록 보기
4/47
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
# 전달인자로 받은 리스트끼리 더해봐서 target 과 
# 일치하는 index 의 리스트를 리턴 
# 클래스의 인스턴스에서 함수를 호출하기 위해 self 포함하여 선언
# 자바스크립트의 this 느낌
# for(let i =0; i<nums.length; i++) => for i in range(0,len(nums),1)
# brute force 방식인 하나하나 대입하는 방식이라 속도가 느림 
        for i in range(0,len(nums),1):
            for j in range(i+1,len(nums),1):
                if (nums[i]+nums[j]==target):
                    return [i,j]
profile
web-developer

0개의 댓글