1、题目

12. Integer to Roman

Roman numerals are represented by seven different symbols: IVXLCD and M.

Symbol       Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000

For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven 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. There are six instances where subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9.
  • X can be placed before L (50) and C (100) to make 40 and 90.
  • C can be placed before D (500) and M (1000) to make 400 and 900.

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.

Example 1:

Input: 3
Output: "III"

Example 2:

Input: 4
Output: "IV"

Example 3:

Input: 9
Output: "IX"

Example 4:

Input: 58
Output: "LVIII"
Explanation: L = 50, V = 5, III = 3.

Example 5:

Input: 1994
Output: "MCMXCIV"
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

2、我的解答(未简化)

 # -*- coding: utf-8 -*-
# @Time : 2020/1/30 19:14
# @Author : SmartCat0929
# @Email : 1027699719@qq.com
# @Link : https://github.com/SmartCat0929
# @Site :
# @File : 12. Integer to Roman(自己想的复杂的算法).py class Solution:
def intToRoman(self, num: int) -> str:
numStr = str(num)
if num < 10:
if int(numStr[-1]) > 0 and int(numStr[-1]) < 4:
units = ""
for i in range(0, int(numStr[-1])):
units = "I" + units
elif int(numStr[-1]) == 4:
units = "IV"
elif int(numStr[-1]) == 5:
units = "V"
elif int(numStr[-1]) > 5 and int(numStr[-1]) < 9:
units = "V"
for i in range(5, int(numStr[-1])):
units = units + "I"
elif int(numStr[-1]) == 9:
units = "IX"
return units
elif num >= 10 and num <= 99: # 如果是两位数
if int(numStr[-1]) == 0:
units = ""
elif int(numStr[-1]) > 0 and int(numStr[-1]) < 4: # 个位
units = ""
for i in range(0, int(numStr[-1])):
units = "I" + units
elif int(numStr[-1]) == 4:
units = "IV"
elif int(numStr[-1]) == 5:
units = "V"
elif int(numStr[-1]) > 5 and int(numStr[-1]) < 9:
units = "V"
for i in range(5, int(numStr[-1])):
units = units + "I"
elif int(numStr[-1]) == 9:
units = "IX"
if int(numStr[-2]) == 1: # 十位
decades = "X"
elif int(numStr[-2]) > 0 and int(numStr[-2]) < 4: # 十位
decades = ""
for j in range(0, int(numStr[-2])):
decades = "X" + decades
elif int(numStr[-2]) == 4:
decades = "XL"
elif int(numStr[-2]) == 5:
decades = "L"
elif int(numStr[-2]) > 5 and int(numStr[-2]) < 9:
decades = "L"
for j in range(5, int(numStr[-2])):
decades = decades + "X"
elif int(numStr[-2]) == 9:
decades = "XC"
romans = decades + units
return romans elif num >= 100 and num <= 999: # 如果是三位数
if int(numStr[-1]) == 0: # 个位
units = ""
elif int(numStr[-1]) > 0 and int(numStr[-1]) < 4:
units = ""
for i in range(0, int(numStr[-1])):
units = "I" + units
elif int(numStr[-1]) == 4:
units = "IV"
elif int(numStr[-1]) == 5:
units = "V"
elif int(numStr[-1]) > 5 and int(numStr[-1]) < 9:
units = "V"
for i in range(5, int(numStr[-1])):
units = units + "I"
elif int(numStr[-1]) == 9:
units = "IX"
if int(numStr[-2]) == 0: # 十位
decades = ""
elif int(numStr[-2]) > 0 and int(numStr[-2]) < 4:
decades = ""
for j in range(0, int(numStr[-2])):
decades = "X" + decades
elif int(numStr[-2]) == 4:
decades = "XL"
elif int(numStr[-2]) == 5:
decades = "L"
elif int(numStr[-2]) > 5 and int(numStr[-2]) < 9:
decades = "L"
for j in range(5, int(numStr[-2])):
decades = decades + "X"
elif int(numStr[-2]) == 9:
decades = "XC"
if int(numStr[-3]) > 0 and int(numStr[-3]) < 4: # 百位
hundreds = ""
for j in range(0, int(numStr[-3])):
hundreds = "C" + hundreds
elif int(numStr[-3]) == 4:
hundreds = "CD"
elif int(numStr[-3]) == 5:
hundreds = "D"
elif int(numStr[-3]) > 5 and int(numStr[-3]) < 9:
hundreds = "D"
for j in range(5, int(numStr[-3])):
hundreds = hundreds + "C"
elif int(numStr[-3]) == 9:
hundreds = "CM"
romans = hundreds + decades + units
return romans elif num >= 1000 and num <= 3999: # 如果是四位数
if int(numStr[-1]) == 0: # 个位
units = ""
elif int(numStr[-1]) > 0 and int(numStr[-1]) < 4:
units = ""
for i in range(0, int(numStr[-1])):
units = "I" + units
elif int(numStr[-1]) == 4:
units = "IV"
elif int(numStr[-1]) == 5:
units = "V"
elif int(numStr[-1]) > 5 and int(numStr[-1]) < 9:
units = "V"
for i in range(5, int(numStr[-1])):
units = units + "I"
elif int(numStr[-1]) == 9:
units = "IX"
if int(numStr[-2]) == 0: # 十位
decades = ""
elif int(numStr[-2]) > 0 and int(numStr[-2]) < 4:
decades = ""
for j in range(0, int(numStr[-2])):
decades = "X" + decades
elif int(numStr[-2]) == 4:
decades = "XL"
elif int(numStr[-2]) == 5:
decades = "L"
elif int(numStr[-2]) > 5 and int(numStr[-2]) < 9:
decades = "L"
for j in range(5, int(numStr[-2])):
decades = decades + "X"
elif int(numStr[-2]) == 9:
decades = "XC"
if int(numStr[-3]) == 0: # 百位
hundreds = ""
elif int(numStr[-3]) > 0 and int(numStr[-3]) < 4:
hundreds = ""
for j in range(0, int(numStr[-3])):
hundreds = "C" + hundreds
elif int(numStr[-3]) == 4:
hundreds = "CD"
elif int(numStr[-3]) == 5:
hundreds = "D"
elif int(numStr[-3]) > 5 and int(numStr[-3]) < 9:
hundreds = "D"
for j in range(5, int(numStr[-3])):
hundreds = hundreds + "C"
elif int(numStr[-3]) == 9:
hundreds = "CM"
if int(numStr[-4]) > 0 and int(numStr[-4]) < 4: #千位
thousands = ""
for j in range(0, int(numStr[-4])):
thousands = "M" + thousands
romans = thousands + hundreds + decades + units
return romans
elif num > 3999:
return 0 print(Solution().intToRoman(400))

3、大神的解法

LeetCode上看到的大神的解法,实在是佩服! 采用字典将特殊符号记住,随后逐一判断.......

 class Solution:
def intToRoman(self, num: int) -> str:
res = ""
s = 1000 d = {1: "I", 5: "V", 10: "X", 50: "L", 100: "C", 500: "D", 1000: "M"}
while num != 0:
r, temp = divmod(num, s) if r == 9:
res += d[s] + d[s * 10]
elif r == 4:
res += d[s] + d[s * 5]
elif r >= 5:
res += d[s * 5] + d[s] * (r - 5)
else:
res += d[s] * r s = s // 10
num = temp return res print(Solution().intToRoman(2964))
												

最新文章

  1. 【转】MessageBox的常见用法
  2. Mac下的串口通信-ORSSerialPort
  3. VS常见错误
  4. UIImageView属性
  5. Codeforces Round #172 (Div. 2) B. Nearest Fraction 二分
  6. [译]CSS content
  7. Codeforces 510B Fox And Two Dots 【DFS】
  8. C#在Json反序列化中处理键的特殊字符
  9. Eclipse无法打开项目中的任何文件
  10. Alpha冲刺No.6
  11. 使用libpcap过滤arp
  12. linux 远程 telnet
  13. HTML文本格式化与HTML 超链接
  14. 给有C或C++基础的Python入门 :Python Crash Course 4 操作列表 4.1--4.3
  15. HOOK IDT频繁蓝屏(Window 正确 HOOK IDT)
  16. Win10, VS2017环境下OpenCV3.4.2的配置
  17. Python 语言来编码和解码 JSON 对象
  18. Redis中在程序中的应用
  19. 关于chrome插件编写的小结
  20. 多选下拉框带搜索(aps.net)

热门文章

  1. Python单元测试unittest与HTMLTestRunner报告生成
  2. JS高级---案例:贪吃蛇小游戏
  3. QT+VS后中文字符乱码问题
  4. 概率dp light 1321
  5. 吴裕雄 python 机器学习——数据预处理标准化MaxAbsScaler模型
  6. Text Infilling解读
  7. Linux - Shell - 字符串拼接
  8. 部署DVWA时的一些问题和解决办法(一)
  9. Cosmetic Airless Bottles To Meet Practical Requirements
  10. Go 语言 fmt.Sprintf (格式化输出)