LeetCode 笔记九 整数与罗马数字的转换


这两个程序是对整数与罗马数字的转换,所以放到一起。

Integer to Roman

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

SymbolValue
I1
V5
X10
L50
C100
D500
M1000

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.

主要注意以上几个临界值的标识,即

SymbolValue
IV4
IX9
XL40
XC90
CD400
CM900

example

example 1

Input: 3
Output: “III”

example 2

Input: 4
Output: “IV”

example 3

Input: 58
Output: “LVIII”
Explanation: L = 50, V = 5, III = 3.

example 4

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

Code

这种题用python编程确实比其他语言简单多了:

class Solution:
    def intToRoman(self, num: int) -> str:
        romanNum = ''
        Mnum = num // 1000
        romanNum = romanNum + Mnum * 'M'
        Mleft = num % 1000
        del Mnum
        
        if Mleft // 100 == 9:
            romanNum = romanNum + 'CM'
            Dleft = Mleft % 900
        else:
            Dnum = Mleft // 500
            romanNum = romanNum + Dnum * 'D'
            Dleft = Mleft % 500
            del Dnum
        
        if Dleft // 100 == 4:
            romanNum = romanNum + 'CD'
            Cleft = Dleft % 400
        else:
            Cnum = Dleft // 100
            romanNum = romanNum + Cnum * 'C' 
            Cleft = Dleft % 100
            del Cnum
        
        if Cleft // 10 == 9:
            romanNum = romanNum + 'XC'
            Lleft = Cleft % 90
        else:
            Lnum = Cleft // 50
            romanNum = romanNum + Lnum * 'L'
            Lleft = Cleft % 50
            del Lnum
        
        if Lleft // 10 == 4:
            romanNum = romanNum + 'XL'
            Xleft = Lleft % 40
        else:
            Xnum = Lleft // 10
            romanNum = romanNum + Xnum * 'X'
            Xleft = Lleft % 10
            del Xnum
        
        if Xleft == 9:
            romanNum = romanNum + 'IX'
        elif Xleft == 4:
            romanNum = romanNum + 'IV'
        else:
            Vnum = Xleft // 5
            romanNum = romanNum + Vnum * 'V'
            del Vnum
            Inum = Xleft % 5
            romanNum = romanNum + Inum * 'I'
            del Inum
        
        del Mleft, Dleft, Cleft, Lleft, Xleft
        return romanNum

结果:

3999 / 3999 test cases passed.
Runtime: 52 ms
Memory Usage: 14.1 MB

Runtime: 52 ms, faster than 86.82% of Python3 online submissions for Integer to Roman.
Memory Usage: 14.1 MB, less than 6.15% of Python3 online submissions for Integer to Roman.

Roman to Integer

就是罗马数字反过来变成整数,规则与整数变为罗马数一样,注意临界值就好。example也跟上面是对应的,就不重复了。

Code

class Solution:
    def romanToInt(self, s: str) -> int:
        romanInt = 0
        if 'IV' in s:
            romanInt += 4
            s = s.replace('IV', '')
        if 'IX' in s:
            romanInt += 9
            s = s.replace('IX', '')
        if 'XL' in s:
            romanInt += 40
            s = s.replace('XL', '')
        if 'XC' in s:
            romanInt += 90
            s = s.replace('XC', '')
        if 'CD' in s:
            romanInt += 400
            s = s.replace('CD', '')
        if 'CM' in s:
            romanInt += 900
            s = s.replace('CM', '')
            
        romanInt = romanInt + s.count('M') * 1000 + s.count('D') * 500 + s.count('C') * 100 + s.count('L') * 50 + s.count('X') * 10 + s.count('V') * 5 + s.count('I') * 1
        return romanInt

结果:

3999 / 3999 test cases passed.
Runtime: 48 ms
Memory Usage: 13.8 MB

Runtime: 48 ms, faster than 92.21% of Python3 online submissions for Roman to Integer.
Memory Usage: 13.8 MB, less than 5.38% of Python3 online submissions for Roman to Integer.

还是挺不错的。
最近好烦啊,不想搞理论的东西,但却在搞理论的东西,而且太难了一直停滞不前,头都秃了。唉人生啊···想要快快乐乐地生活着可太艰难了。接下来还有一个比赛也要开始了,之前的比赛没有记录,以后也顺便记录下吧。
生活啊,又有谁不难呢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值