leedCode练题——12. Integer to Roman

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 : [email protected]
# @Link : https://github.com/SmartCat0929
# @Site :
# @File : 12. Integer to Roman(simplify).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))

猜你喜欢

转载自www.cnblogs.com/Smart-Cat/p/12244011.html