//GitHub https://github.com/vellimole0621
vellimole0621 - Overview
キム・ヒョンギュ . vellimole0621 has 6 repositories available. Follow their code on GitHub.
github.com
12. Integer to Roman
https://leetcode.com/problems/integer-to-roman/
Integer to Roman - LeetCode
Can you solve this real interview question? Integer to Roman - Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, 2 is written as II in Roman numeral, just tw
leetcode.com
문제 설명
-
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX.
Given an integer, convert it to a roman numeral.
=> 주어진 숫자를 로마자로 변환하라.
내 풀이
=> 노가다로 각각 큰 자리수 순으로 찾음
class Solution:
def intToRoman(self, num: int) -> str:
num_box = []
if num >= 1000:
num_thousand = int(num / 1000)
for i in range(0, num_thousand):
num_box.append("M")
if num >= 100:
if num >= 1000:
num_hundred = int((num / 100) % 10)
else:
num_hundred = int(num / 100)
if num_hundred == 9:
num_box.append("CM")
elif num_hundred == 4:
num_box.append("CD")
elif num_hundred > 5 and num_hundred != 9:
num_box.append("D")
for j in range(0, num_hundred-5):
num_box.append("C")
elif num_hundred == 5:
num_box.append("D")
else:
for k in range(0, num_hundred):
num_box.append("C")
if num >= 10:
if num >= 100:
num_ten = int((num / 10) % 10)
else:
num_ten = int(num / 10)
if num_ten == 9:
num_box.append("XC")
elif num_ten == 4:
num_box.append("XL")
elif num_ten > 5 and num_ten != 9:
num_box.append("L")
for j in range(0, num_ten - 5):
num_box.append("X")
elif num_ten == 5:
num_box.append("L")
else:
for k in range(0, num_ten):
num_box.append("X")
if num >= 1:
if num >= 10:
num_one = int(num% 10)
else:
num_one = num
if num_one == 9:
num_box.append("IX")
elif num_one == 4:
num_box.append("IV")
elif num_one > 5 and num_one != 9:
num_box.append("V")
for j in range(0, num_one - 5):
num_box.append("I")
elif num_one == 5:
num_box.append("V")
else:
for k in range(0, num_one):
num_box.append("I")
ans = ''.join(num_box)
return ans
너무 성능이 떨어지는 풀이여서. 더 효과적인 풀이를 찾아보았다.
출처
[Python 으로 푸는 Leetcode]12. Integer to Roman
https://velog.io/@kgh732/Python-%EC%9C%BC%EB%A1%9C-%ED%91%B8%EB%8A%94-Leetcode12.-Integer-to-Roman
[Python 으로 푸는 Leetcode]12. Integer to Roman
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.For example, 2 is written as II in Roman numeral, just two one's add
velog.io
class Solution:
def intToRoman(self, num: int) -> str:
value2roman={1000:'M',900:'CM',500:'D',400:'CD',100:'C',90:'XC',50:'L',40:'XL',10:'X',9:'IX',5:'V',4:'IV',1:'I'}
roman=''
while num!=0:
for value in value2roman:
if num-value >=0:
roman+=value2roman[value]
num=num-value
break
return roman
알고리즘 풀이 전, 접근 방식에 대해서 충분히 고려하고 풀이를 시작해야겠다...
'프로그래밍 > Python' 카테고리의 다른 글
[파이썬 Python/알고리즘] LeetCode - Fizz Buzz (0) | 2023.07.31 |
---|---|
[파이썬 Python/알고리즘] LeetCode - Richest Customer Wealth (0) | 2023.07.30 |
[파이썬 Python/알고리즘] LeetCode - Valid Perfect Square (0) | 2023.07.19 |
[파이썬 Python/알고리즘] LeetCode - Unique Paths (0) | 2023.07.16 |
[파이썬 Python/알고리즘] LeetCode - Single Number II (0) | 2023.07.04 |