[LeetCode]Roman to Integer

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

根本不知道罗马数字怎么个表达方法,下面摘自维基百科。

Roman Numerals, as used today, are based on seven symbols:[1]

Symbol Value
I1
V5
X10
L50
C100
D500
M1,000

Numbers are formed by combining symbols and adding the values. So II is two ones, i.e. 2, and XIII is a ten and three ones, i.e. 13. There is no zero in this system, so 207, for example, is CCVII, using the symbols for two hundreds, a five and two ones. 1066 is MLXVI, one thousand, fifty and ten, a five and a one.

Symbols are placed from left to right in order of value, starting with the largest. However, in a few specific cases,[2] to avoid four characters being repeated in succession (such as IIII or XXXX) these can be reduced usingsubtractive notation as follows:[3][4]

  • the numeral I can be placed before V and X to make 4 units (IV) and 9 units (IX respectively)
  • X can be placed before L and C to make 40 (XL) and 90 (XC respectively)
  • C can be placed before D and M to make 400 (CD) and 900 (CM) according to the same pattern[5]

An example using the above rules would be 1904: this is composed of 1 (one thousand), 9 (nine hundreds), 0 (zero tens), and 4 (four units). To write the Roman numeral, each of the non-zero digits should be treated separately. Thus 1,000 = M, 900 = CM, and 4 = IV. Therefore, 1904 is MCMIV.

Below are some examples of the modern use of Roman Numerals.

想想这个罗马数字不被我们熟悉是有原因的,智商跟阿拉伯人确实没有可比性,现在只用在表上做装饰用。代码简单粗暴。

/*
I	1
V	5
X	10
L	50
C	100
D	500
M	1,000
*/
class Solution {
public:
    int romanToInt(string s) {
        int sum = 0;
        for (int i = 0; i < s.length(); i++)
        {
            if (s[i] == 'I')
            {
                if (i + 1 < s.length())
                {
                    if (s[i + 1] == 'V')
                    {
                        sum += 3;
                        i++;
                    }
                    else if (s[i + 1] == 'X')
                    {
                        sum += 8;
                        i++;
                    }
                }
                sum += 1;
            }
            else if (s[i] == 'X')
            {
                if (i + 1 < s.length())
                {
                    if (s[i + 1] == 'L')
                    {
                        sum += 30;
                        i++;
                    }
                    else if (s[i + 1] == 'C')
                    {
                        sum += 80;
                        i++;
                    }
                }
                sum += 10;
            }
            else if (s[i] == 'C')
            {
                if (i + 1 < s.length())
                {
                    if (s[i + 1] == 'D')
                    {
                        sum += 300;
                        i++;
                    }
                    else if (s[i + 1] == 'M')
                    {
                        sum += 800;
                        i++;
                    }
                }
                sum += 100;
            }
            else if (s[i] == 'V')
                sum += 5;
            else if (s[i] == 'L')
                sum += 50;
            else if (s[i] == 'D')
                sum += 500;
            else if (s[i] == 'M')
                sum += 1000;
        }
        return sum;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值