13. Roman to Integer

https://leetcode.com/problems/roman-to-integer/description/
题目大意:罗马数字转整数
解题思路1:暴力,一次遍历字符串,根据所有规则一步步判断

class Solution:
    def romanToInt(self, s):
        """
        :type s: str
        :rtype: int
        """
        thousands, hundreds, tens, ones = 0, 0, 0, 0
        n = len(s)
        i = 0
        while i < n and s[i] == 'M':
            thousands += 1
            i += 1
        if i+1 < n and s[i] == 'C' and s[i+1] == 'D':
            hundreds = 4
            i += 2
        elif i+1 < n and s[i] == 'C' and s[i+1] == 'M':
            hundreds = 9
            i += 2
        elif i < n and s[i] == 'D':
            i += 1
            hundreds = 5
            while i < n and s[i] == 'C':
                hundreds += 1
                i += 1
        else:
            while i < n and s[i] == 'C':
                hundreds += 1
                i += 1

        if i+1 < n and s[i] == 'X' and s[i+1] == 'L':
            tens = 4
            i += 2
        elif i+1 < n and s[i] == 'X' and s[i+1] == 'C':
            tens = 9
            i += 2
        elif i < n and s[i] == 'L':
            i += 1
            tens = 5
            while i < n and s[i] == 'X':
                tens += 1
                i += 1
        else:
            while i < n and s[i] == 'X':
                tens += 1
                i += 1

        if i+1 < n and s[i] == 'I' and s[i+1] == 'V':
            ones = 4
            i += 2
        elif i+1 < n and s[i] == 'I' and s[i+1] == 'X':
            ones = 9
            i += 2
        elif i < n and s[i] == 'V':
            i += 1
            ones = 5
            while i < n and s[i] == 'I':
                ones += 1
                i += 1
        else:
            while i < n and s[i] == 'I':
                ones += 1
                i += 1

        return thousands * 1000 + hundreds * 100 + tens * 10 + ones

思路2(比较6666):根据“左减右加”。把所有字符转成相应数字,按原有顺序存入数组中,例如字符串s = “MCMXCIV”,每次比较s[i]与s[i+1]的数字大小,若s[i]的数字比s[i+1]大,如第一个M,就可以直接将其累加到sum。若s[i]的数字比s[i+1]小,根据“左减右加”,sum减去s[i+1]即可

class Solution:
    def romanToInt(self, s):
        """
        :type s: str
        :rtype: int
        """
        res = 0
        nums = [0] * len(s)  #注意数组定义时要指定长度,不能直接创建一个空list,否则无法直接用下标添加元素

        for i in range(len(s)):
            if s[i] == 'I':
                nums[i] = 1
            elif s[i] == 'V':
                nums[i] = 5
            elif s[i] == 'X':
                nums[i] = 10
            elif s[i] == 'L':
                nums[i] = 50
            elif s[i] == 'C':
                nums[i] = 100
            elif s[i] == 'D':
                nums[i] = 500
            elif s[i] == 'M':
                nums[i] = 1000

        for i in range(len(s) - 1):
            if nums[i] < nums[i+1]:
                res -= nums[i]
            else:
                res += nums[i]

        return res + nums[len(s) - 1]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值