leetCode练题——12. Integer to Roman
1、题目
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000
For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
Ican be placed beforeV(5) andX(10) to make 4 and 9.Xcan be placed beforeL(50) andC(100) to make 40 and 90.Ccan be placed beforeD(500) andM(1000) to make 400 and 900.
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 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 and IV = 4.
2、我的解答(未简化)
1 # -*- coding: utf-8 -*- 2 # @Time : 2020/1/30 19:14 3 # @Author : SmartCat0929 4 # @Email : 1027699719@qq.com 5 # @Link : https://github.com/SmartCat0929 6 # @Site : 7 # @File : 12. Integer to Roman(自己想的复杂的算法).py 8 9 class Solution: 10 def intToRoman(self, num: int) -> str: 11 numStr = str(num) 12 if num < 10: 13 if int(numStr[-1]) > 0 and int(numStr[-1]) < 4: 14 units = "" 15 for i in range(0, int(numStr[-1])): 16 units = "I" + units 17 elif int(numStr[-1]) == 4: 18 units = "IV" 19 elif int(numStr[-1]) == 5: 20 units = "V" 21 elif int(numStr[-1]) > 5 and int(numStr[-1]) < 9: 22 units = "V" 23 for i in range(5, int(numStr[-1])): 24 units = units + "I" 25 elif int(numStr[-1]) == 9: 26 units = "IX" 27 return units 28 elif num >= 10 and num <= 99: # 如果是两位数 29 if int(numStr[-1]) == 0: 30 units = "" 31 elif int(numStr[-1]) > 0 and int(numStr[-1]) < 4: # 个位 32 units = "" 33 for i in range(0, int(numStr[-1])): 34 units = "I" + units 35 elif int(numStr[-1]) == 4: 36 units = "IV" 37 elif int(numStr[-1]) == 5: 38 units = "V" 39 elif int(numStr[-1]) > 5 and int(numStr[-1]) < 9: 40 units = "V" 41 for i in range(5, int(numStr[-1])): 42 units = units + "I" 43 elif int(numStr[-1]) == 9: 44 units = "IX" 45 if int(numStr[-2]) == 1: # 十位 46 decades = "X" 47 elif int(numStr[-2]) > 0 and int(numStr[-2]) < 4: # 十位 48 decades = "" 49 for j in range(0, int(numStr[-2])): 50 decades = "X" + decades 51 elif int(numStr[-2]) == 4: 52 decades = "XL" 53 elif int(numStr[-2]) == 5: 54 decades = "L" 55 elif int(numStr[-2]) > 5 and int(numStr[-2]) < 9: 56 decades = "L" 57 for j in range(5, int(numStr[-2])): 58 decades = decades + "X" 59 elif int(numStr[-2]) == 9: 60 decades = "XC" 61 romans = decades + units 62 return romans 63 64 elif num >= 100 and num <= 999: # 如果是三位数 65 if int(numStr[-1]) == 0: # 个位 66 units = "" 67 elif int(numStr[-1]) > 0 and int(numStr[-1]) < 4: 68 units = "" 69 for i in range(0, int(numStr[-1])): 70 units = "I" + units 71 elif int(numStr[-1]) == 4: 72 units = "IV" 73 elif int(numStr[-1]) == 5: 74 units = "V" 75 elif int(numStr[-1]) > 5 and int(numStr[-1]) < 9: 76 units = "V" 77 for i in range(5, int(numStr[-1])): 78 units = units + "I" 79 elif int(numStr[-1]) == 9: 80 units = "IX" 81 if int(numStr[-2]) == 0: # 十位 82 decades = "" 83 elif int(numStr[-2]) > 0 and int(numStr[-2]) < 4: 84 decades = "" 85 for j in range(0, int(numStr[-2])): 86 decades = "X" + decades 87 elif int(numStr[-2]) == 4: 88 decades = "XL" 89 elif int(numStr[-2]) == 5: 90 decades = "L" 91 elif int(numStr[-2]) > 5 and int(numStr[-2]) < 9: 92 decades = "L" 93 for j in range(5, int(numStr[-2])): 94 decades = decades + "X" 95 elif int(numStr[-2]) == 9: 96 decades = "XC" 97 if int(numStr[-3]) > 0 and int(numStr[-3]) < 4: # 百位 98 hundreds = "" 99 for j in range(0, int(numStr[-3])): 100 hundreds = "C" + hundreds 101 elif int(numStr[-3]) == 4: 102 hundreds = "CD" 103 elif int(numStr[-3]) == 5: 104 hundreds = "D" 105 elif int(numStr[-3]) > 5 and int(numStr[-3]) < 9: 106 hundreds = "D" 107 for j in range(5, int(numStr[-3])): 108 hundreds = hundreds + "C" 109 elif int(numStr[-3]) == 9: 110 hundreds = "CM" 111 romans = hundreds + decades + units 112 return romans 113 114 elif num >= 1000 and num <= 3999: # 如果是四位数 115 if int(numStr[-1]) == 0: # 个位 116 units = "" 117 elif int(numStr[-1]) > 0 and int(numStr[-1]) < 4: 118 units = "" 119 for i in range(0, int(numStr[-1])): 120 units = "I" + units 121 elif int(numStr[-1]) == 4: 122 units = "IV" 123 elif int(numStr[-1]) == 5: 124 units = "V" 125 elif int(numStr[-1]) > 5 and int(numStr[-1]) < 9: 126 units = "V" 127 for i in range(5, int(numStr[-1])): 128 units = units + "I" 129 elif int(numStr[-1]) == 9: 130 units = "IX" 131 if int(numStr[-2]) == 0: # 十位 132 decades = "" 133 elif int(numStr[-2]) > 0 and int(numStr[-2]) < 4: 134 decades = "" 135 for j in range(0, int(numStr[-2])): 136 decades = "X" + decades 137 elif int(numStr[-2]) == 4: 138 decades = "XL" 139 elif int(numStr[-2]) == 5: 140 decades = "L" 141 elif int(numStr[-2]) > 5 and int(numStr[-2]) < 9: 142 decades = "L" 143 for j in range(5, int(numStr[-2])): 144 decades = decades + "X" 145 elif int(numStr[-2]) == 9: 146 decades = "XC" 147 if int(numStr[-3]) == 0: # 百位 148 hundreds = "" 149 elif int(numStr[-3]) > 0 and int(numStr[-3]) < 4: 150 hundreds = "" 151 for j in range(0, int(numStr[-3])): 152 hundreds = "C" + hundreds 153 elif int(numStr[-3]) == 4: 154 hundreds = "CD" 155 elif int(numStr[-3]) == 5: 156 hundreds = "D" 157 elif int(numStr[-3]) > 5 and int(numStr[-3]) < 9: 158 hundreds = "D" 159 for j in range(5, int(numStr[-3])): 160 hundreds = hundreds + "C" 161 elif int(numStr[-3]) == 9: 162 hundreds = "CM" 163 if int(numStr[-4]) > 0 and int(numStr[-4]) < 4: #千位 164 thousands = "" 165 for j in range(0, int(numStr[-4])): 166 thousands = "M" + thousands 167 romans = thousands + hundreds + decades + units 168 return romans 169 elif num > 3999: 170 return 0 171 172 173 print(Solution().intToRoman(400))
3、大神的解法
LeetCode上看到的大神的解法,实在是佩服! 采用字典将特殊符号记住,随后逐一判断.......
1 class Solution: 2 def intToRoman(self, num: int) -> str: 3 res = "" 4 s = 1000 5 6 d = {1: "I", 5: "V", 10: "X", 50: "L", 100: "C", 500: "D", 1000: "M"} 7 while num != 0: 8 r, temp = divmod(num, s) 9 10 if r == 9: 11 res += d[s] + d[s * 10] 12 elif r == 4: 13 res += d[s] + d[s * 5] 14 elif r >= 5: 15 res += d[s * 5] + d[s] * (r - 5) 16 else: 17 res += d[s] * r 18 19 s = s // 10 20 num = temp 21 22 return res 23 24 25 print(Solution().intToRoman(2964))

    
                
            
        
浙公网安备 33010602011771号