【leetcode】13. 罗马数字转整数

我的解法

感觉是字符串处理

class Solution {
    public int romanToInt(String s) {
        int ans = 0;

        for(int i = 0;i < s.length();++i){
            if(s.charAt(i) == 'I')
                ans += 1;
            if(s.charAt(i) == 'V'){
                if(i != 0 && s.charAt(i-1)=='I')
                    ans += 3;
                else
                    ans += 5;
            }
            if(s.charAt(i) == 'X'){
                if(i != 0 && s.charAt(i-1) == 'I')
                    ans += 8;
                else
                    ans += 10;
            }
            if(s.charAt(i) == 'L'){
                if(i != 0 && s.charAt(i-1) == 'X')
                     ans += 30;
                else
                     ans += 50;
            }
            if(s.charAt(i) == 'C'){
                if(i != 0 && s.charAt(i-1) == 'X')
                     ans += 80;
                else
                     ans += 100;
            }
            if(s.charAt(i) == 'D'){
                if(i != 0 && s.charAt(i-1) == 'C')
                     ans += 300;
                else
                     ans += 500;
            }
            if(s.charAt(i) == 'M'){
                if(i != 0 && s.charAt(i-1) == 'C')
                    ans += 800;
                else
                    ans += 1000;
            }                
            
        }


        return ans;
    }
}

正着需要“回顾”前一个字符,倒着需要“回顾”后一个字符,比如左边是 I ,则减1,否则加1.

class Solution {
    public int romanToInt(String s) {
        int ans = 0;

        char pre = '/';
        for(int i = s.length()-1;i >= 0;--i){
            if(s.charAt(i) == 'I'){
                if(pre == 'V' || pre == 'X'){
                    ans -= 1;
                }else
                    ans += 1;
            }
                // ans += 1;
            if(s.charAt(i) == 'V'){
                ans += 5;
                // if(i != 0 && s.charAt(i-1)=='I')
                //     ans += 3;
                // else
                //     ans += 5;
            }
            if(s.charAt(i) == 'X'){
                if(pre == 'L' || pre == 'C')
                    ans -= 10;
                else
                   ans += 10;
                // if(i != 0 && s.charAt(i-1) == 'I')
                //     ans += 8;
                // else
                //     ans += 10;
            }
            if(s.charAt(i) == 'L'){
                ans += 50;
                // if(i != 0 && s.charAt(i-1) == 'X')
                //      ans += 30;
                // else
                //      ans += 50;
            }
            if(s.charAt(i) == 'C'){
                if(pre == 'D' || pre == 'M')
                    ans -= 100;
                else
                    ans += 100;
                // if(i != 0 && s.charAt(i-1) == 'X')
                //      ans += 80;
                // else
                //      ans += 100;
            }
            if(s.charAt(i) == 'D'){
                ans += 500;
                // if(i != 0 && s.charAt(i-1) == 'C')
                //      ans += 300;
                // else
                //      ans += 500;
            }
            if(s.charAt(i) == 'M'){
                ans += 1000;
                // if(i != 0 && s.charAt(i-1) == 'C')
                //     ans += 800;
                // else
                //     ans += 1000;
            }                
            pre = s.charAt(i);
        }


        return ans;
    }
}

在这里插入图片描述
倒着比正着快一点

题解

class Solution {
private:
    unordered_map<char, int> symbolValues = {
        {'I', 1},
        {'V', 5},
        {'X', 10},
        {'L', 50},
        {'C', 100},
        {'D', 500},
        {'M', 1000},
    };

public:
    int romanToInt(string s) {
        int ans = 0;
        int n = s.length();
        for (int i = 0; i < n; ++i) {
            int value = symbolValues[s[i]];
            if (i < n - 1 && value < symbolValues[s[i + 1]]) {
                ans -= value;
            } else {
                ans += value;
            }
        }
        return ans;
    }
};

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/roman-to-integer/solution/luo-ma-shu-zi-zhuan-zheng-shu-by-leetcod-w55p/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

他默认输入合法序列(即不存在 ID 这种情况),所以左逢 I 必减1,否则必加1。X,C同理。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AmosTian

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值