Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Use a greedy approach by iterating over Roman values from largest to smallest, repeatedly subtracting the biggest possible value and appending its symbol.
- Subtractive cases like 4 (IV), 9 (IX), 40 (XL), etc., are pre-stored in the mapping to naturally handle special Roman numeral rules.
- """
- class Solution:
- def intToRoman(self, num: int) -> str:
- dict = {1000:'M',900:'CM',500:'D',400:'CD',100:'C',90:'XC',50:'L',40:'XL',10:'X',9:'IX',5:'V',4:'IV',1:'I'}
- print(dict)
- ans=''
- while num>0:
- for i in dict.keys():
- if i<=num:
- ans+=dict[i]
- num-=i
- break
- return ans
Advertisement
Add Comment
Please, Sign In to add comment