@@ -10,10 +10,31 @@ def intToRoman(self, num):
1010 :param num:
1111 :return:
1212 """
13- num2roman = {1 : 'I' , 5 : 'V' , 10 : 'X' , 50 : 'L' , 100 : 'C' , 500 : 'D' , 1000 : 'M' }
14- rets = ['I' , 'V' , 'X' , 'L' , 'C' , 'D' , 'M' ]
15- for n in num2roman :
16- num
13+ roman2num = {'I' : 1 , 'V' : 5 , 'X' : 10 , 'L' : 50 , 'C' : 100 , 'D' : 500 , 'M' : 1000 }
14+ roman_digits = ['I' , 'V' , 'X' , 'L' , 'C' , 'D' , 'M' ]
15+ s = ''
16+ for idx , roman_digit in enumerate (roman_digits [::- 1 ], 1 ):
17+ idx = len (roman_digits ) - idx
18+ if num <= 0 :
19+ break
20+
21+ digit = num // roman2num [roman_digit ]
22+ if digit :
23+ if idx < len (roman_digits ) - 1 :
24+ if idx > 0 and idx % 2 and (num + roman2num [roman_digits [idx - 1 ]]) // roman2num [
25+ roman_digits [idx + 1 ]] == 1 :
26+ s += roman_digits [idx - 1 ] + roman_digits [idx + 1 ]
27+ num -= roman2num [roman_digits [idx + 1 ]] - roman2num [roman_digits [idx - 1 ]]
28+ elif (idx + 1 ) % 2 and (num + roman2num [roman_digit ]) // roman2num [roman_digits [idx + 1 ]] == 1 :
29+ s += roman_digits [idx ] + roman_digits [idx + 1 ]
30+ num -= roman2num [roman_digits [idx + 1 ]] - roman2num [roman_digit ]
31+ else :
32+ s += roman_digits [idx ] * digit
33+ num -= digit * roman2num [roman_digits [idx ]]
34+ else :
35+ s += roman_digits [idx ] * digit
36+ num -= digit * roman2num [roman_digits [idx ]]
37+ return s
1738
1839 def threeSumClosest (self , nums : List [int ], target : int ) -> int :
1940 """
@@ -30,7 +51,7 @@ def threeSumClosest(self, nums: List[int], target: int) -> int:
3051 return sum (nums [:3 ])
3152 if sum (nums [- 3 :]) <= target :
3253 return sum (nums [- 3 :])
33- min_diff = sum (nums [:3 ])- target
54+ min_diff = sum (nums [:3 ]) - target
3455 for i in range (len (nums ) - 2 ):
3556 j = i + 1
3657 k = len (nums ) - 1
0 commit comments