링크
https://leetcode.com/problems/two-sum/
정답 1
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[j] == target - nums[i]:
return [i, j]
정답 2
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
hashmap = {}
for i in range(len(nums)):
hashmap[nums[i]] = i
for i in range(len(nums)):
complement = target - nums[i]
if complement in hashmap and hashmap[complement] != i:
return [i, hashmap[complement]]
정답 3
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
hashmap = {}
for i, num in enumerate(nums):
complement = target - num
if complement in hashmap:
return [hashmap[complement], i]
hashmap[num] = i
'[업무 지식] > Python' 카테고리의 다른 글
| [A/B test] 온라인 / 대면 학습 (0) | 2025.02.13 |
|---|---|
| [SMTP] 이메일 자동화 (0) | 2025.02.13 |
| [Funnel Analysis] 홈페이지 - 장바구니 - 결제에 따른 퍼널 변화 (0) | 2025.02.13 |
| [Cohort Analysis] 첫 거래후 다른 카테고리 확장 구매 고객 vs 동일 카테고리에 머무른 고객 (0) | 2025.02.13 |
| [기본 탐색] 기본 데이터 확인 코드 (1) | 2025.01.08 |