[Leetcode] [Python] Integer to Roman

Source: Internet
Author: User

#-*-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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.