13-->>python3

Source: Internet
Author: User

The Roman numerals contain the following seven characters:,,,, I V X L C , D and M .

Character          value I             1V             5X             10L             50C             100D             500M             1000

For example, the Roman numeral 2 is written II , that is, two parallel 1. 12 Write XII , that is X + II . 27 Write XXVII , that is XX + V + II .

Usually, the number of Roman numerals is small and the numbers are on the right side of the large numbers. But there are exceptions, such as 4 not IIII to write, but IV . The number 1 is on the left side of the number 5, the number represented is equal to the large number 5 decreases the number 1 gets the value 4. Similarly, the number 9 is represented as IX . This special rule applies only to the following six cases:

    • ICan be placed on V X the left side of (5) and (10) to represent 4 and 9.
    • XCan be placed on L C the left side of (50) and (100) to represent 40 and 90.
    • CCan be placed on D M the left side of (500) and (1000) to represent 400 and 900.

Given a Roman number, convert it to an integer. The input is guaranteed to be within the range of 1 to 3999.

Example 1:

Input: "III" output: 3

Example 2:

Input: "IV" output: 4

Example 3:

Input: "IX" output: 9

Example 4:

Input: "LVIII" output: 58 Explanation: C = +, L = +, XXX = +, III = 3.

Example 5:

Input: "MCMXCIV" output: 1994 Explanation: M = +, CM = =, XC = all, IV = 4.
Analysis: required to convert Roman numerals to decimal numbers, first we need to know the mechanism of conversion.   If the input is a letter (Roman number), then the direct return of the letter represents the decimal number if it is two or more: starting from the first (i) to find out the judgment, if I represents the decimal number is greater than the i+1 represents, the sum, conversely, minus I represents the decimal number.  This problem clearly requires the decimal number corresponding to the Roman numerals to be marked, and in Python, the dictionary operation can be easily solved. Use a dictionary to mark, and then parse and convert a given Roman number.
1 classSolution:2     defRomantoint (self, s):3         """4 : Type S:str5 : Rtype:int6         """7tag={'M': 1000,'D': 500,'C': 100,'L': 50,'X': 10,'V'75A'I': 1}8sum=09         ifLen (s) ==1:Ten             returnTag[s[-1]] One          forIinchRange (len (s)-1): A             ifTag[s[i]]<tag[s[i+1] ]: #tag [s[i]] denotes the key value of the letter -sum-=Tag[s[i]] -             Else: thesum+=Tag[s[i]] -              -         returnSUM+TAG[S[-1]]

13-->>python3

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.