xiejunzhao

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.


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
class Solution:
    def intToRoman(self, num):
        """
        :type num: int
        :rtype: str
        """
        I = [\'\', \'I\', \'II\', \'III\', \'IV\', \'V\', \'VI\', \'VII\', \'VIII\', \'IX\']
        X = [\'\', \'X\', \'XX\', \'XXX\', \'XL\', \'L\', \'LX\', \'LXX\', \'LXXX\', \'XC\']
        C = [\'\', \'C\', \'CC\', \'CCC\', \'CD\', \'D\', \'DC\', \'DCC\', \'DCCC\', \'CM\']
        M = [\'\', \'M\', \'MM\', \'MMM\']
        thousand = num // 1000
        houndred = (num % 1000) // 100
        ten = (num % 100) // 10
        digit = num % 10
        return M[thousand] + C[houndred] + X[ten] + I[digit]
  
  
s = Solution()
# num = 1
# num = 19
# num = 99
# num = 100
num = 409
#num = 1999
res = s.intToRoman(num)
print(res)






分类:

技术点:

相关文章:

  • 2022-01-25
  • 2022-12-23
  • 2022-12-23
  • 2021-05-26
  • 2021-07-08
  • 2021-06-17
  • 2021-11-18
  • 2021-09-21
猜你喜欢
  • 2021-11-23
  • 2022-01-28
  • 2022-02-21
  • 2022-01-12
  • 2021-12-08
  • 2022-12-23
  • 2022-01-08
相关资源
相似解决方案