Skip to content

Instantly share code, notes, and snippets.

@rdtr
Created January 4, 2016 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rdtr/85317d0891208216f02c to your computer and use it in GitHub Desktop.
Save rdtr/85317d0891208216f02c to your computer and use it in GitHub Desktop.
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