Leetcode 012 整数转罗马数字 思路详解+总结易错 Python

本人一直在努力地积累Leetcode上用Python实现的题,并且会尽力讲清每道题的原理,绝不像其他某些博客简略地带过。

如果觉得讲的清楚,欢迎关注。


题目:

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: C = 100, L = 50, XXX = 30 and III = 3.

Example 5:

Input: 1994
Output: "MCMXCIV"
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.



思路:首先我们要明白这题的范围。它只能是小于4000的整数。所以输入一定是个n位数(n<=4)。然后就可以顺理成章地把这个数思考为一个长度为4的list,缺少的数字用0替代。判断第一位数字大小,更新要返回的结果。总之因为数字所在的Index代表了它的权重,因此比较方便我们加上正确的值。总之这道题在我看来无非多了一些条件判断。

80%beat:

class Solution:
    def intToRoman(self, num):
        """
        :type num: int
        :rtype: str
        """
        numlen = len(str(num))
        number = list(str(num))
        for i in range(4-numlen):
            number = [0] + number
        number = list(map(int, number))
        result = ""
        if number[0] != 0:
            result += 'M'*number[0]
        if number[1] != 0:
            if number[1] == 4:
                result += 'CD'
            elif number[1] == 9:
                result += 'CM'
            elif number[1] < 5:
                result += 'C' * number[1]
            else:
                result += ('D' + "C"*(number[1] - 5))
        if number[2] != 0:
            if number[2] == 4:
                result += 'XL'
            elif number[2] == 9:
                result += 'XC'
            elif number[2] < 5:
                result += 'X'*number[2]
            else:
                result += ('L' + 'X'*(number[2]-5))
        if number[3] != 0:
            if number[3] == 4:
                result += 'IV'
            elif number[3] == 9:
                result += 'IX'
            elif number[3] < 5:
                result += 'I'*number[3]
            else:
                result += ('V'+'I'*(number[3]- 5)) 

        return result
        
总结:这道题易错在哪呢?易错在没有分类讨论4和9这种特殊数字

猜你喜欢

转载自blog.csdn.net/weixin_41958153/article/details/80617233