Tow Sum

Tasic·2021년 1월 19일
0

Algorithm

목록 보기
1/4

Question

TwoSum
주어진 숫자 배열중에 두 수를 더해서 만족 하는 숫자(Target) 되는 Index를 구해라

Code

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        arrLen =  len(nums) 
        for i in range(0, arrLen-1) :
            for j in range(1, arrLen) :
                if i == j :
                    continue

                sumValue = nums[i] + nums[j]
                if sumValue == target :
                    return [i, j]

Result

Runtime: 48 ms, faster than 62.52% of Python3 online submissions for Two Sum.
Memory Usage: 14.5 MB, less than 45.84% of Python3 online submissions for Two Sum.

간단하게 이중 for문 돌려서 했는데 가장 기본 적인 방법인듯 하다.
더 최적할 방법이 있는지 찾아봐야됨.

profile
블로그 옮겼습니다 (https://jotasic.github.io)

0개의 댓글