罗马数字转整数

<h4 style="box-sizing: border-box; font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: 500; line-height: 1.1; color: rgb(113, 113, 113); margin-top: 10px; margin-bottom: 10px; font-size: 18px;"><span class="m-l-sm m-t-sm" style="box-sizing: border-box; margin-left: 10px; margin-top: 10px;">罗马数字转整数</span></h4><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(113, 113, 113); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;">给定一个罗马数字,将其转换成整数。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(113, 113, 113); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;">返回的结果要求在1到3999的范围内。</p><div class="line line-dashed" style="box-sizing: border-box; height: 2px; margin: 10px 0px; font-size: 0px; overflow: hidden; border-width: 1px 0px 0px; border-style: dashed; border-top-color: rgb(234, 237, 239); color: rgb(113, 113, 113); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"></div><span class="text-sm" style="box-sizing: border-box; color: rgb(113, 113, 113); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif;">您在真实的面试中是否遇到过这个题?</span><span style="color: rgb(113, 113, 113); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"> </span><div class="btn-group" data-toggle="buttons" style="box-sizing: border-box; position: relative; display: inline-block; vertical-align: middle; color: rgb(113, 113, 113); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"><div class="btn btn-white btn-xs" id="yes" style="box-sizing: border-box; display: inline-block; padding: 1px 5px; margin-bottom: 0px; font-size: 12px; line-height: 1.5; text-align: center; white-space: nowrap; vertical-align: middle; cursor: pointer; -webkit-user-select: none; border: 1px solid rgba(150, 160, 180, 0.298039); border-radius: 3px; box-shadow: rgba(0, 0, 0, 0.0470588) 0px -1px 1px inset; position: relative; float: left; margin-left: 0px; background-image: none; background-clip: padding-box;">Yes</div></div><span style="color: rgb(113, 113, 113); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"></span><span style="color: rgb(113, 113, 113); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"></span><span style="color: rgb(113, 113, 113); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"></span><div class="m-t-lg m-b-lg" style="box-sizing: border-box; margin-top: 20px; margin-bottom: 20px; color: rgb(113, 113, 113); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"><span style="box-sizing: border-box;">样例</span><div class="m-t-sm" style="box-sizing: border-box; margin-top: 10px;"><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;"><code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); white-space: nowrap; border-radius: 4px; background-color: rgb(249, 242, 244);">IV</code> -> <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); white-space: nowrap; border-radius: 4px; background-color: rgb(249, 242, 244);">4</code></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;"><code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); white-space: nowrap; border-radius: 4px; background-color: rgb(249, 242, 244);">XII</code> -> <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); white-space: nowrap; border-radius: 4px; background-color: rgb(249, 242, 244);">12</code></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;"><code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); white-space: nowrap; border-radius: 4px; background-color: rgb(249, 242, 244);">XXI</code> -> <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); white-space: nowrap; border-radius: 4px; background-color: rgb(249, 242, 244);">21</code></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;"><code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); white-space: nowrap; border-radius: 4px; background-color: rgb(249, 242, 244);">XCIX</code> -> <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); white-space: nowrap; border-radius: 4px; background-color: rgb(249, 242, 244);">99</code></p></div></div>

int romanToInt(string& s) {
        // Write your code here
        if(s.length() == 0) {
            return 0;
        }
        unordered_map<char, int> m;
        m['I'] = 1;
	    m['V'] = 5;
	    m['X'] = 10;
	    m['L'] = 50;
	    m['C'] = 100;
	    m['D'] = 500;
	    m['M'] = 1000;
	    
        int num = m[s.at(s.length() - 1)];
        for(int i = s.length()-2; i >= 0; i--){
            if(m[s.at(i)] >= m[s.at(i+1)]) {
                num += m[s.at(i)];
            }
            else {
                num -= m[s.at(i)];
            }
        }
        return num;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值