Leetcode_12 [Integer to Roman numerals]

Article Directory:

  • topic
  • A script and notes
  • 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 an integer, convert it to a Roman numeral. To ensure that the input is in the range of 1 to 3999.

Example 1:

Input: 3
Output: "III"
Example 2:

Input: 4
Output: "IV"
Example 3:

Input: 9
Output: "IX"
Example 4:

Input: 58
Output: "LVIII"
explanation: L = 50, V = 5 , III = 3.
Example 5:

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


And a script Note: [when using: 48ms]

class Solution:
     DEF intToRoman (Self, num: int) -> STR: Title # define the output and an output, the input integer, and the variable named num outputs a string variable
        num1 NUM # 1000 value% = modulo 
        count1 is = int (NUM / 1000 ) acquires the input value # 1000 contains the number of
        num2 num1 # 100% and then modulo value = 
        count2 = int (num1 / 100 ) # I acquired value after taking 100 contains the number
        num3 num2% # 10 and then take the value I = 
        count3 = int (num2 / 10 ) # I acquired value after taking the number 10 contains
        List1 = [ ' M ' , ' the MM ' , ' MMM ' ] definition list #
        List2 = [ ' C ' , ' the CC ' , ' the CCC ' , ' the CD ' , ' D ' , ' the DC ' , ' the DCC ' , ' DCCC ' , ' the CM ' ] definition list #
        list3 = [ ' X- ' , ' XX ' , ' XXX ' , ' XL ' , ' L ' , ' LX ' , ' LXX ' , ' LXXX ' , ' XC ' ] definition list #
        list4 = [ ' the I ' , ' II ' , ' III ' , ' IV ' , ' V ' , ' Vl ' , ' VII ' , ' VIII ' , ' IX ' ] definition list #
        STR = "" # define the empty string 
        IF count1 is =! 0: Analyzing # 1000 contains the number is zero, the process proceeds to decision statement if zero
            STR = + STR List1 [count1 is -. 1 ] # The variable value, an empty string with a list of elements 
         IF count2 =! 0: # 100 contains the number determined is zero, the process proceeds to decision statement if zero
            STR = + STR List2 [count2 -. 1 ] # The value of the variable, the string with a list of elements
         IF count3 =! 0: # supra
            STR = + STR list3 [count3 -. 1 ] # supra
         IF ! num3 = 0: # supra
            STR = + STR list4 [num3 -1 ] # supra
         return (STR) # After determination is completed, return the string variable
class Solution:
    def intToRoman(self, num: int) -> str:
        Num1 = 1000% the num 
        Count1 = an int ( the num / 1000 )
        num2 = num1 % 100
        count2 = int(num1 / 100)
        num3 = num2 % 10
        count3 = int(num2 / 10)
        list1 = ['M','MM','MMM']
        list2 = ['C','CC','CCC','CD','D','DC','DCC','DCCC','CM']
        List3 = [ ' 10 ' , ' 20 ' , ' 30 ' , ' 40 ' , ' 50 ' , ' 60 ' , ' 70 ' , ' 80 ' , ' 90 ' ]
        list4 = [ ' I ' , ' II ' , ' III ' , ' IV ' , ' V ' , ' VI ' , ' VII ' , ' VIII ' , ' IX ' ]
        str = ""
        if count1 != 0:
            str = str + list1[count1 - 1]
        if count2 != 0:
            str = str + list2[count2 - 1]
        if count3 != 0:
            str = str + list3[count3 - 1]
        if num3 != 0:
            str = str + list4[num3 -1]
        return(str)
Uncommented Code

A script logic:

  • First: This range is known to be in question entered digit
  • Second: this conversion, in fact, the value of each band corresponds to a specific value, which is fixed, so it can be fixed to a list of Roman numerals corresponding hexadecimal; for example, tens of 1,2,3, Roman numerals correspond 4,5,6,7,8,9 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC '
  • Third: Just get value for each hexadecimal, and converted to a specific element in the list can be solved

 

 

Guess you like

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