티스토리 뷰
문제

덧셈하여 타겟(target)을 만들 수 있는 배열의 두 숫자 인덱스를 리턴하라
코드
from typing import List
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
nums_map = {}
# 키와 값을 바꿔서 딕셔너리로 저장
for i, num in enumerate(nums):
nums_map[num] = i
# 타겟에서 첫 번째 수를 뺀 결과를 키로 조회
for i, num in enumerate(nums):
if target - num in nums_map and i != nums_map[target-num]:
return nums.index(num), nums_map[target-num]
'알고리즘' 카테고리의 다른 글
[algorithm] leetcode 21 - Merge Two Sorted Lists (파이썬) (0) | 2022.03.15 |
---|---|
[algorithm] leetcode 121 - Best Time to Buy and Sell Stock (파이썬) (0) | 2022.03.14 |
[algorithm] leetcode 937 - Reorder Data in Log Files (파이썬) (0) | 2022.03.11 |
[algorithm] leetcode 344 - reverse-string (파이썬) (0) | 2022.03.10 |
[algorithm] leetcode 561 - Array Partition I (파이썬) (0) | 2022.03.09 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- mysql 8.0
- Spring
- leetcode
- 코테
- 백준
- 스프링
- 자료구조
- 리팩토링
- 스프링부트
- 데이터베이스
- Algorithm
- 노마드
- webflux
- 릿코드
- 노마드코더
- 인프런
- 김영한
- Real MySQL
- 그리디
- 정렬
- 코틀린
- 구현
- kotlin
- 파이썬
- 스프링 부트
- MySQL
- spring boot
- 문자열
- 알고리즘
- 북클럽
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함