Roman to Integer

Given a roman numeral, convert it to an integer.

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

从前向后遍历罗马数字,如果某个数比前一个数小,则加上该数。反之,减去前一个数的两倍然后加上该数。

class Solution {
public:
    int romanToInt(string s) {
       int result=0;  
          
        map<char,int> roman;  
        roman['I']=1;  
        roman['V']=5;  
        roman['X']=10;  
        roman['L']=50;  
        roman['C']=100;  
        roman['D']=500;  
        roman['M']=1000;  
        result=roman[s[0]];
        for(int i=1;i<s.length();i++){
            int current=roman[s[i]];
            int previous=roman[s[i-1]];
            if(current<=previous){
                result=result+current;
            } else {
                result=result+current-2*previous;
            }
            
        }
        return result;
    }
};

其他:

class Solution {
public:
    int romanToInt(string s) {
        int ret = toNumber(s[0]);
        for (int i = 1; i < s.length(); i++) {
            if (toNumber(s[i - 1]) < toNumber(s[i])) {
                ret += toNumber(s[i]) - 2 * toNumber(s[i - 1]);
            } else {
                ret += toNumber(s[i]);
            }
        }
        return ret;
    }
    
    int toNumber(char ch) {
        switch (ch) {
            case 'I': return 1;
            case 'V': return 5;
            case 'X': return 10;
            case 'L': return 50;
            case 'C': return 100;
            case 'D': return 500;
            case 'M': return 1000;
        }
        return 0;
    }
};

羅馬數字共有7個,即I(1)、V(5)、X(10)、L(50)、C(100)、D(500)和M(1000)。按照下述的規則可以表示任意正整數。需要注意的是罗马数字中没有“0”,與進位制無關。一般認為羅馬數字只用來記數,而不作演算。

  • 重複數次:一個羅馬數字重複幾次,就表示這個數的幾倍。
  • 右加左減:
    • 在較大的羅馬數字的右邊記上較小的羅馬數字,表示大數字加小數字。
    • 在較大的羅馬數字的左邊記上較小的羅馬數字,表示大數字减小數字。
    • 左减的数字有限制,仅限于I、X、C。比如45不可以写成VL,只能是XLV
    • 但是,左減時不可跨越一個位數。比如,99不可以用IC(100 - 1)表示,而是用XCIX([100 - 10] + [10 - 1])表示。(等同於阿拉伯數字每位數字分別表示。)
    • 左減數字必須為一位,比如8寫成VIII,而非IIX。
    • 右加數字不可連續超過三位,比如14寫成XIV,而非XIIII。(見下方“數碼限制”一項。)
  • 加線乘千:
    • 在羅馬數字的上方加上一條橫線或者加上下標的Ⅿ,表示將這個數乘以1000,即是原數的1000倍。
    • 同理,如果上方有兩條橫線,即是原數的1000000(1000^{2})倍。
  • 數碼限制:
    • 同一數碼最多只能出現三次,如40不可表示為XXXX,而要表示為XL。
    • 例外:由於IV是古羅馬神話主神朱庇特(即IVPITER,古羅馬字母裡沒有J和U)的首字,因此有時用IIII代替Ⅳ。


1. Roman to Integer

[cpp]  view plain copy
  1. class Solution {  
  2. public:  
  3.     int romanToInt(string s) {  
  4.         // Note: The Solution object is instantiated only once and is reused by each test case.  
  5.         int result=0;  
  6.           
  7.         map<char,int> roman;  
  8.         roman['I']=1;  
  9.         roman['V']=5;  
  10.         roman['X']=10;  
  11.         roman['L']=50;  
  12.         roman['C']=100;  
  13.         roman['D']=500;  
  14.         roman['M']=1000;  
  15.           
  16.         for(int i=s.length()-1;i>=0;i--)  
  17.         {  
  18.             if(i==s.length()-1)  
  19.             {  
  20.                 result=roman[s[i]];  
  21.                 continue;  
  22.             }  
  23.             if(roman[s[i]] >= roman[s[i+1]])  
  24.                 result+=roman[s[i]];  
  25.             else  
  26.                 result-=roman[s[i]];  
  27.         }  
  28.         return result;  
  29.     }  
  30. };  

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值