Created
January 4, 2016 16:45
-
-
Save rdtr/85317d0891208216f02c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution(object): | |
def intToRoman(self, num): | |
""" | |
:type num: int | |
:rtype: str | |
""" | |
# mp = {scale: {number : roman}} | |
mp = {1: ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'], | |
10: ['', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC'], | |
100: ['', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM'], | |
1000: ['', 'M', 'MM', 'MMM']} | |
res = '' | |
base = 1000 | |
while num > 0: | |
digit = num / base | |
res += mp[base][digit] | |
num -= digit * base | |
base /= 10 | |
return res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment