Table of Content
题目:
这道题给出两种解法 :
一种解法是通过条件判断求解,代码如下:
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 |
''' Created on: Saturday, September 01, 2018 @author: Jedi Liu ''' class Solution: def intToRoman(self, num): """ :type num: int :rtype: str """ n = num roman = '' m = n // 1000 if m > 0: n = n - m * 1000 while m > 0: m = m - 1 roman = roman + 'M' c = n // 100 if c > 0: if c == 9: n = n - 900 roman = roman + 'CM' elif c >= 5: n = n - 500 roman = roman + 'D' c = c - 5 n = n - c * 100 while c > 0: c = c - 1 roman = roman + 'C' elif c == 4: n = n - 400 roman = roman + 'CD' elif c > 0: n = n - c * 100 while c > 0: c = c - 1 roman = roman + 'C' x = n // 10 if x > 0: if x == 9: n = n - 90 roman = roman + 'XC' elif x >= 5: n = n - 50 roman = roman + 'L' x = x - 5 n = n - x * 10 while x > 0: x = x - 1 roman = roman + 'X' elif x == 4: n = n - 40 roman = roman + 'XL' elif x > 0: n = n - x * 10 while x > 0: x = x - 1 roman = roman + 'X' if n == 9: roman = roman + 'IX' elif n >= 5: n = n - 5 roman = roman + 'V' while n > 0: n = n - 1 roman = roman + 'I' elif n == 4: roman = roman + 'IV' else: while n > 0: roman = roman + 'I' n = n - 1 return roman s = Solution() print(s.intToRoman(27)) |
这种方法效率有点差,代码也不够简练,但容易想到。下面给出第二种解法,这种解法是利用罗马数字和阿拉伯数字的对应关系求解,代码如下:
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 |
''' Created on: Saturday, September 01, 2018 @author: Jedi Liu ''' class Solution1: def intToRoman(self, num): """ :type num: int :rtype: str """ n = num roman = "" div = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1] romans = [ "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" ] while n != 0: for i in range(len(div)): d = div[i] times = n // d if times != 0: roman = roman + (times) * romans[i] n = n - d * times return roman |
如果你有更好的实现方法,欢迎交流。