9/25 每日一題(羅馬數字 整數轉羅馬)
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 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. There are six instances where subtraction is used:
Ican be placed beforeV(5) andX(10) to make 4 and 9.Xcan be placed beforeL(50) andC(100) to make 40 and 90.Ccan be placed beforeD(500) andM(1000) to make 400 and 900.
Given an integer, convert it to a roman numeral.
Example 1:
Input: num = 3 Output: "III" Explanation: 3 is represented as 3 ones.
Example 2:
Input: num = 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3.
Example 3:
Input: num = 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
class Solution:
def intToRoman(self, num: int) -> str:
val=[1000,900,500,400,100,90,50,40,10,9,5,4,1]
symbol=['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I']
n=0
res=[]
while n < len(val):
if num < val[n]:
n+=1
else:
print(f'目前num={num}')
print(f'目前n={n}')
roma=num//val[n]
data=roma*symbol[n]
print(f'餘數={roma}, data={data} ')
res.append(data)
num -= roma*val[n]
print(f'res={res}')
return ''.join(res)
------------------------------------
class Solution:
def intToRoman(self, num: int) -> str:
val = [1000,900,500,400,100,90,50,40,10,9,5,4,1]
symbol = ['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I']
result = ""
for i in range(len(val)):
while num >= val[i]:
result += symbol[i]
num -= val[i]
return result
class Solution:
def intToRoman(self, num: int) -> str:
def int_2_rom(n):
if n >= 1000:
c = n // 1000
return c * 'M' + int_2_rom(n-(c*1000))
if n >= 900:
return 'CM' + int_2_rom(n-900)
if n >= 500:
return 'D' + int_2_rom(n-500)
if n >= 400:
return 'CD' + int_2_rom(n-400)
if n >= 100:
c = n // 100
return c * 'C' + int_2_rom(n-(c*100))
if n >= 90:
return 'XC' + int_2_rom(n-90)
if n >= 50:
return 'L' + int_2_rom(n-50)
if n >= 40:
return 'XL' + int_2_rom(n-40)
if n >= 10:
c = n // 10
return c * 'X' + int_2_rom(n-(c*10))
if n >= 9:
return 'IX' + int_2_rom(n-9)
if n >= 5:
return 'V' + int_2_rom(n - 5)
if n >= 4:
return 'IV' + int_2_rom(n - 4)
else:
return n * 'I'
return int_2_rom(num)------------------------------
class Solution:
def intToRoman(self, num: int) -> str:
result = ''
while num != 0:
if num >= 1000:
result += "M"
num -= 1000
elif num >= 900:
result += 'CM'
num -= 900
elif num >= 500:
result += "D"
num -= 500
elif num >= 400:
result += 'CD'
num -= 400
elif num >= 100:
result += 'C'
num -= 100
elif num >= 90:
result += 'XC'
num -= 90
elif num >= 50:
result += 'L'
num -= 50
elif num >= 40:
result += 'XL'
num -= 40
elif num >= 10:
result += 'X'
num -= 10
elif num >= 9:
result += 'IX'
num -= 9
elif num >= 5:
result += 'V'
num -= 5
elif num >= 4:
result += 'IV'
num -= 4
else:
result += 'I'
num -= 1
return result----------------------------------
標籤: leetcode

0 個意見:
張貼留言
訂閱 張貼留言 [Atom]
<< 首頁