-
Notifications
You must be signed in to change notification settings - Fork 43
/
integer-to-roman.py
215 lines (185 loc) · 6.43 KB
/
integer-to-roman.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
"""
12. Integer to Roman
Medium
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, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 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:
I can be placed before V (5) and X (10) to make 4 and 9.
X can be placed before L (50) and C (100) to make 40 and 90.
C can be placed before D (500) and M (1000) to make 400 and 900.
Given an integer, convert it to a roman numeral.
Example 1:
Input: num = 3
Output: "III"
Explanation: 3 is represented as 3 ones.
Example 2:
Input: num = 58
Output: "LVIII"
Explanation: L = 50, V = 5, III = 3.
Example 3:
Input: num = 1994
Output: "MCMXCIV"
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
Constraints:
1 <= num <= 3999
"""
# V0
# IDEA
# In [8]: 1200%100
# Out[8]: 0
# In [9]: 1234%1000
# Out[9]: 234
# In [10]: 1234%100
# Out[10]: 34
# In [11]: 1234%10
# Out[11]: 4
# example :
# def str_2_int(x):
# r=0
# for i in x:
# r = int(r)*10 + int(i)
# print (i, r)
# return r
class Solution(object):
def intToRoman(self, num):
M = ["", "M", "MM", "MMM"]
C = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"]
X = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"]
I = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"]
return M[num/1000] + C[(num%1000)/100] + X[(num%100)/10] + I[num%10]
# V1
# https://blog.csdn.net/NXHYD/article/details/72461202
class Solution(object):
def intToRoman(self, num):
"""
:type num: int
:rtype: str
"""
c = [['','I','II','III','IV','V','VI','VII','VIII','IX'],
['','X','XX','XXX','XL','L','LX','LXX','LXXX','XC'],
['','C','CC','CCC','CD','D','DC','DCC','DCCC','CM'],
['','M','MM','MMM']]
s = []
i = 3
while num :
s.append(c[i][num/pow(10, i)])
num %= pow(10, i)
i -= 1
return ''.join(s)
# V1
# IDEA : Greedy
# https://leetcode.com/problems/integer-to-roman/solution/
class Solution:
def intToRoman(self, num: int) -> str:
digits = [(1000, "M"), (900, "CM"), (500, "D"), (400, "CD"), (100, "C"),
(90, "XC"), (50, "L"), (40, "XL"), (10, "X"), (9, "IX"),
(5, "V"), (4, "IV"), (1, "I")]
roman_digits = []
# Loop through each symbol.
for value, symbol in digits:
# We don't want to continue looping if we're done.
if num == 0: break
count, num = divmod(num, value)
# Append "count" copies of "symbol" to roman_digits.
roman_digits.append(symbol * count)
return "".join(roman_digits)
# V1
# IDEA : Hardcode Digits
# https://leetcode.com/problems/integer-to-roman/solution/
class Solution:
def intToRoman(self, num: int) -> str:
thousands = ["", "M", "MM", "MMM"]
hundreds = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"]
tens = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"]
ones = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"]
return (thousands[num // 1000] + hundreds[num % 1000 // 100]
+ tens[num % 100 // 10] + ones[num % 10])
# V1'
# https://blog.csdn.net/NXHYD/article/details/72461202
class Solution(object):
def intToRoman(self, num):
"""
:type num: int
:rtype: str
"""
M = ["", "M", "MM", "MMM"]
C = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"]
X = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"]
I = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"]
return M[num/1000] + C[(num%1000)/100] + X[(num%100)/10] + I[num%10]
# V1''
# https://www.jiuzhang.com/solution/integer-to-roman/#tag-highlight-lang-python
class Solution:
def parse(self, digit, index):
NUMS = {
1: 'I',
2: 'II',
3: 'III',
4: 'IV',
5: 'V',
6: 'VI',
7: 'VII',
8: 'VIII',
9: 'IX',
}
ROMAN = {
'I': ['I', 'X', 'C', 'M'],
'V': ['V', 'L', 'D', '?'],
'X': ['X', 'C', 'M', '?']
}
s = NUMS[digit]
return s.replace('X', ROMAN['X'][index]).replace('I', ROMAN['I'][index]).replace('V', ROMAN['V'][index])
# @param {integer} num
# @return {string}
def intToRoman(self, num):
s = ''
index = 0
while num != 0:
digit = num % 10
if digit != 0:
s = self.parse(digit, index) + s
num = num / 10
index += 1
return s
# V2
# https://blog.csdn.net/NXHYD/article/details/72461202
class Solution(object):
def intToRoman(self, num):
import math
"""
:type num: int
:rtype: str
"""
M = ["", "M", "MM", "MMM"]
C = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"]
X = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"]
I = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"]
return M[(math.floor(num/1000))] + C[(math.floor(num%1000)/100)] + X[(math.floor(num%100)/10)] + I[(math.floor(num%10))]
# V3
# Time: O(n)
# Space: O(1)
class Solution(object):
def intToRoman(self, num):
"""
:type num: int
:rtype: str
"""
numeral_map = {1: "I", 4: "IV", 5: "V", 9: "IX", \
10: "X", 40: "XL", 50: "L", 90: "XC", \
100: "C", 400: "CD", 500: "D", 900: "CM", \
1000: "M"}
keyset, result = sorted(numeral_map.keys()), []
while num > 0:
for key in reversed(keyset):
while num / key > 0:
num -= key
result += numeral_map[key]
return "".join(result)