Roman to Integer

来源:互联网 发布:达梦数据库公司上市 编辑:程序博客网 时间:2024/06/02 03:56

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 charToInt(char c)    {    switch(c)    {    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;    default:    return 0;    }    }    int romanToInt(string s) {        int result = 0;    for (int i = 0; i < s.length(); i++)    {    result = result + charToInt(s[i]) - 2*(result % charToInt(s[i]));    }    return result;    }};


0 0
原创粉丝点击