#-*-Coding:utf8-*-
‘‘‘
__author__ = ' [email protected] '
https://oj.leetcode.com/problems/integer-to-roman/
Integer to Roman
Given an integer, convert it to a Roman numeral.
Input is guaranteed to being within the range from 1 to 3999.
===comments by dabay===
First Google a little bit of Roman numerals:
I-1
V-5
X-10
L-50
C-100
D-500
M-1000
The main problem is that there will be some 4,40 and the like.
For example, 999, because 900 can be expressed as cm, so the need for Mr. CM, the number is reduced by 900 to 99, instead of 500 to 499.
You can generate an ordered list of key values, reducing the maximum each time until the remaining number is less than the maximum number in the hash table, and the maximum number is deleted to continue processing.
‘‘‘
Class Solution:
# @return A string
def inttoroman (self, num):
pairs = [
(+, "M"),
("CM"),
("D"),
("CD"),
("C"),
("XC"),
("L"),
(+, "XL"),
(Ten, "X"),
(9, "IX"),
(5, "V"),
(4, "IV"),
(1, "I")
]
res = ""
For (n, s) in pairs:
While Num >= N:
res = res + S
num = Num-n
return res
def main ():
s = solution ()
Print S.inttoroman (6)
if __name__ = = "__main__":
Import time
Start = Time.clock ()
Main ()
Print "%s sec"% (Time.clock ()-start)
[Leetcode][python]integer to Roman