【leetcode】(python) 12. Integer to Roman解题思路

Description:
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:

  • 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. 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.

题目大意:
将罗马数字转换为对应的整数

解题思路:
字典法
将规则对应的罗马数字和整数放到字典中,通过遍历字典用待转换整数对数字求商,商的值就是最大的罗马数字个数,将其保存到结果中,同理,对操作后的待转换余数进行同样处理。

注意:对应的字典序列一定要从大到小,否则,遍历字典时任何数除以1总是得到的I

class Solution:
    def intToRoman(self, num: int) -> str:
        Inter = ['1', '4', '5', '9', '10', '40', '50', '90', '100', '400', '500', '900', '1000']
        Roma = ['I', 'IV', 'V', 'IX', 'X', 'XL', 'L', 'XC', 'C', 'CD', 'D', 'CM', 'M']
        # reversed()求逆序
        l = dict(zip(reversed(Inter), reversed(Roma)))

        res= ''
        for k, v in l.items():
            temp = num // int(k)
            if temp != 0:
                res += temp * v
                num -= temp * int(k)
        return res

按位计算法
得到待转化整数的个、十、百、千位。同样也是从高位到低位处理,千位数字不超过4,依题意大于4000的数未作讨论,若是小于等于4则千位直接对应M。百位上1到9情况各不相同,分别处理即可。十位个位类似。

class Solution:
    def intToRoman(self, num: int) -> str:
        if (num >= 4000):
            return ""
        r=""
        d = num % 10
        num = num // 10
        t = num % 10
        num = num // 10
        h = num % 10
        num = num // 10
        th = num % 10
        num = num // 10
        
        if th :
            while (th > 0):
                r = r + "M"
                th = th - 1
        if h :
            if h == 9 :
                r = r+"CM"
            elif h == 8 :
                r = r+"DCCC"
            elif h == 7 :
                r = r + "DCC"
            elif h == 6 :
                r = r+"DC"
            elif h == 5 :
                r = r + "D"
            elif h == 4:
                r = r + "CD"
            elif h == 3:
                r = r + "CCC"
            elif h == 2:
                r = r + "CC"
            elif h == 1:
                r = r + "C"
        if t :
            if t == 9:
                r = r + "XC"
            elif t == 8:
                r = r + "LXXX"
            elif t == 7 :
                r = r + "LXX"
            elif t == 6 :
                r = r + "LX"
            elif t == 5 :
                r = r + "L"
            elif t == 4 :
                r = r + "XL"
            elif t == 3 :
                r = r + "XXX"
            elif t == 2 :
                r = r + "XX"
            elif t == 1 :
                r = r + "X"
        if d :
            if d == 9:
                r = r + "IX"
            elif d == 8:
                r = r + "VIII"
            elif d == 7 :
                r = r + "VII"
            elif d == 6 :
                r = r + "VI"
            elif d == 5 :
                r = r + "V"
            elif d == 4 :
                r = r + "IV"
            elif d == 3 :
                r = r + "III"
            elif d == 2 :
                r = r + "II"
            elif d == 1 :
                r = r + "I"
        
        return (r)

代码优化:

class Solution:
    def intToRoman(self, num: int) -> str:
        
        """
        :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];

参考:https://www.jianshu.com/p/0a3c4cf37195

你可能感兴趣的:(【leetcode】(python) 12. Integer to Roman解题思路)