Leetcode_13 Roman numeral to Integer]

Article Directory:

  • topic
  • A script and notes
    •   Uncommented script
  • A script logic

 

topic:

Roman numeral characters comprising the following seven: I, V, X, L, C, D and M.

Numerical character
the I. 1
V. 5
X-10
L 50
C 100
D 500
M 1000
, for example, 2 written as Roman numerals II, namely 1.12 written as two parallel XII, namely X + II. 27 written as XXVII, namely XX + V + II.

Typically, small numbers of Roman numerals to the right in large numbers. But there are exceptions, for example, do not write 4 IIII, but IV. In the left number 5 number 1, the number of large numbers equal to the value represented by a reduced number 5 obtained 4. Likewise, the number 9 is represented as IX. This special rule applies only to the following six cases:

I may be on the left V (5) and X (10), and to represent 4 and 9.
X L can be placed on the left (50) and C (100), and 40 and 90 are represented. 
C may be placed on D (500) and M (1000) to the left, to 400 and 900 represent.
Given a Roman numeral, to convert it to an integer. To ensure that the input is in 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
explains: L = 50, V = 5 , III = 3.
Example 5:

Input: "MCMXCIV"
Output: 1994
explanation: M = 1000, CM = 900 , XC = 90, IV = 4.


A script and notes:

class Solution:
     DEF romanToInt (Self, S: STR) -> int: 
        SUM1 = s.count ( ' the I ' ) + s.count ( ' V ' ) * + s.count. 5 ( ' X- ' ) * 10 + S .count ( ' L ' ) * 50 + s.count ( ' C ' ) * 100 + s.count ( ' D ' ) * 500 + s.count ( ' M ' ) * 1000 
# Roman numerals of acquiring literally values and jianshu = s.count ( ' IV ' ) * 2 + s.count ( 'IX ') * 2 + s.count ( ' XL ' ) * + 20 is s.count ( ' XC ' ) * + 20 is s.count ( ' the CD ' ) * + 200 is s.count ( ' the CM ' ) * 200 is
# get input Roman numeral value of the sum of the special representative of the string return (SUM1 - jianshu)
# the literal sum minus the sum of special characters
class Solution:
    def romanToInt(self, s: str) -> int:
        sum1 = s.count('I') + s.count('V') * 5 + s.count('X') * 10 + s.count('L') * 50 + s.count('C') * 100 + s.count('D') * 500 + s.count('M') * 1000
        jianshu = s.count('IV') * 2 + s.count('IX') * 2 + s.count('XL') * 20 + s.count('XC') * 20 + s.count('CD') * 200 + s.count('CM') * 200
        return(sum1 - jianshu)
Uncommented Code

A script logic:

  • Get its first representative value based on each character Roman
  • Due to the special string Roman characters, similar to the "IX" literal value of 10 + 1, it is actually representative of 9; Roman character string should be calculated so that the input string contains special number, then the literal minus the sum of the values ​​represented by the sum of the special characters
  • Note should subtract the sum of the two representatives of special characters

 

Guess you like

Origin www.cnblogs.com/mailong/p/12008526.html