zoukankan      html  css  js  c++  java
  • Roman to Integer

    Given a roman numeral, convert it to an integer.

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

    Code:

    class Solution {
    public:
        int romanToInt(string s) {
            int len=s.length();
            map<char,int> character;
            character['I'] = 1;
            character['V'] = 5;
            character['X'] = 10;
            character['L'] = 50;
            character['C'] = 100;
            character['D'] = 500;
            character['M'] = 1000;
            int sum=character[s[len-1]];
            for(int i=0;i<len-1;i++){
                if(character[s[i]]>=character[s[i+1]])
                    sum+=character[s[i]];
                else
                    sum-=character[s[i]];
            }
            return sum;
        }
    };
  • 相关阅读:
    第52周二Restful
    第52周一
    第51周日
    第51周六
    第51周五
    第51周四
    第51周三
    第51周二
    第51周一
    第50周日
  • 原文地址:https://www.cnblogs.com/winscoder/p/3414731.html
Copyright © 2011-2022 走看看