Skip to content

Commit fc8b5ed

Browse files
committed
feat: integer-to-roman
1 parent 90e73f6 commit fc8b5ed

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

str.integer-to-roman.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
"""
3+
12. 整数转罗马数字
4+
https://leetcode-cn.com/problems/integer-to-roman/
5+
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。
6+
给定一个整数,将其转为罗马数字。输入确保在 1 到 3999 的范围内。
7+
"""
8+
def intToRoman(self, num: int) -> str:
9+
nums = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
10+
romans = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
11+
index = 0
12+
res = ''
13+
while index < 13:
14+
if num >= nums[index]:
15+
res += romans[index]
16+
num -= nums[index]
17+
else:
18+
index += 1
19+
20+
return res
21+
22+
23+
so = Solution()
24+
print(so.intToRoman(58))

0 commit comments

Comments
 (0)