Leetcode|Leetcode 12. Integer to Roman
Python 3 实现:
【Leetcode|Leetcode 12. Integer to Roman】源代码已上传 Github,持续更新。
"""
12. Integer to RomanGiven an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.
"""class Solution:
def intToRoman(self, num):
"""
:type num: int
:rtype: str
"""integers = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
romans = ["M","CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
i = 0
result = ""while i < len(integers):
if num >= integers[i]:
result = result + romans[i]
num = num - integers[i]
i -= 1i += 1return resultif __name__ == '__main__':solution = Solution()
print(solution.intToRoman(2))
推荐阅读
- 【Leetcode/Python】001-Two|【Leetcode/Python】001-Two Sum
- leetcode|leetcode 92. 反转链表 II
- gitlab|Gitlab升级(12.2.1到14.6.4)
- 2020-12(完成事项)
- 二叉树路径节点关键值和等于目标值(LeetCode--112&LeetCode--113)
- LeetCode算法题-11.|LeetCode算法题-11. 盛最多水的容器(Swift)
- 咨询手记(12.4)
- LeetCode(03)Longest|LeetCode(03)Longest Substring Without Repeating Characters
- Leetcode|Leetcode No.198打家劫舍
- 2020.12.8