일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 영어원서읽기
- 잡생각
- 쓰릴오브파이트
- 리얼 클래스
- English
- 미드시청
- 읽기
- 만화도
- 개발자
- 30분
- Problem Solving
- FIT XR
- Daily Challenge
- 운동
- 링피트
- 화상영어
- 매일
- 뭐든
- Writing
- leetcode
- 파비최
- 영어공부
- 월간
- realclass
- 10분
- 프로젝트
- 스탭퍼
- 괜찮음
- 사이드
- 3줄정리
Archives
- Today
- Total
파비의 매일매일 공부기록
Today's Challenge 본문
https://leetcode.com/problems/integer-to-roman/
Integer to Roman - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
뒤에서부터 앞으로 순회하면서 풀면 될 것 같긴 한데..
솔루션을 보니 뒤에서 가 아니라 큰 수부터 빼면 된다.
class Solution:
def intToRoman(self, num: int) -> str:
num_map = {
1: "I", 5: "V", 4: "IV",
10: "X", 9: "IX",
50: "L", 40: "XL",
100: "C", 90: "XC",
500: "D", 400: "CD",
1000: "M", 900: "CM",
}
res = ''
for n in [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]:
while n <= num:
res += num_map[n]
num -= n
return res
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
Today's Challenge (0) | 2022.10.22 |
---|---|
Today's Challenge (0) | 2022.10.21 |
Today's Challenge (0) | 2022.10.19 |
Today's Challenge (0) | 2022.10.18 |
Today's Challenge (0) | 2022.10.17 |
Comments