[LeetCode] Roman to Integer

原创 2015年07月09日 16:43:25

使用map

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

    }
};

c语言同样的思路

int romanToInt(char *s) {
#define _M 1000
#define _D 500
#define _C 100
#define _L 50
#define _X 10
#define _V 5
#define _I 1

    int result  = 0;
    int i;
    int last = _M;
    for(i = 0;i < strlen(s);i++)
    {
        switch (s[i])
        {
            case 'M':
                result = (last >= _M ? result + _M:result + _M - (last << 1));
                last = _M;        
            break;
            case 'D':
                result = (last >= _D ? result + _D:result + _D - (last << 1));
                last = _D;
            break;
            case 'C':
                result = (last >= _C ? result + _C:result + _C - (last << 1));
                last = _C;
            break;
            case 'L':
                result = (last >= _L ? result + _L:result + _L - (last << 1));
                last = _L;
            break;
            case 'X':
                result = (last >= _X ? result + _X:result + _X - (last << 1));
                last = _X;
            break;
            case 'V':
                result = (last >= _V ? result + _V:result + _V - (last << 1));
                last = _V;
            break;
            case 'I':
                result = (last >= _I ? result + _I:result + _I - (last << 1));
                last = _I;
            break;
        }

    }
    return result;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

leetcode/roman to integer

如今我们最常见的罗马数字就是钟表的表盘符号:Ⅰ,Ⅱ,Ⅲ,Ⅳ(IIII),Ⅴ,Ⅵ,Ⅶ,Ⅷ,Ⅸ,Ⅹ,Ⅺ,Ⅻ…… 对应阿拉伯数字(就是现在国际通用的数字),就是1,2,3,4,5,6,7,8,9,10...

LeetCode 13_Roman to Integer

首先说明一下,这道题和前面12题是双胞胎,以至于刚开始我一度怀疑这是用来凑数的,不过后来分析得知二者用的并不是同一算法。而且在最后整理的过程中还经历了一些有趣的波折,到最后总结时才发现,这些话也是重新...

LeetCode Roman to Integer

题目 Given a roman numeral,convert it to an integer. Input is guaranteed to bewithin the range from ...

13 leetcode - Roman to Integer

Roman to Integer:Given a roman numeral, convert it to an integer.

leetcode 12 -- Integer to Roman

Integer to Roman 题目: Given an integer, convert it to a roman numeral. Input is guaranteed to...

LeetCode之13---Roman to Integer

题目:   Given a roman numeral, convert it to an integer.   Input is guaranteed to be within the range ...

【leetcode】13. Roman to Integer

一、题目描述 Given a roman numeral, convert it to an integer. Input is guaranteed to be within the rang...

LeetCode013:Roman to Integer

原题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range...

Leetcode 12. Integer to Roman

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 ...

LeetCode 13. Roman to Integer

package com.gloomy.leetcode;import java.util.HashMap;/** * 罗马数字转换为阿拉伯数字 * * @author 52sqz 罗马数字是阿...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)