[LeetCode][Python]Integer to Roman

1631 ワード

# -*- 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 be within the range from 1 to 3999.
===Comments by Dabay===
google :
I - 1
V - 5
X - 10
L - 50
C - 100
D - 500
M - 1000
4,40 。
999 , 900 CM, CM, 900 99; 500 499.
, , hash , 。
'''

class Solution:
# @return a string
def intToRoman(self, num):
pairs = [
(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")
]
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)