LeetCode:Roman to Integer

 

Given a roman numeral, convert it to an integer.

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

Subscribe to see which companies asked this question

 1 class Solution {
 2 public:
 3     int romanToInt(string s) {
 4         map<char,int> dict;
 5         dict['I'] = 1, dict['i'] = 1;
 6         dict['V'] = 5, dict['v'] = 5;
 7         dict['X'] = 10, dict['x'] = 10;
 8         dict['L'] = 50, dict['l'] = 50;
 9         dict['C'] = 100, dict['c'] = 100;
10         dict['D'] = 500, dict['d'] = 500;
11         dict['M'] = 1000, dict['m'] = 1000;
12         
13         int sum=0;
14         for(int i=0;i<s.size();i++)
15         {
16            int j=i+1;
17            if(j<s.size()&&dict[s[i]]<dict[s[j]])
18            {
19                 sum+=dict[s[j]]-dict[s[i]];
20                 i=j;
21            }
22            else
23                 sum+=dict[s[i]];
24         }
25         return sum;
26     
27         
28     }
29 };

 

转载于:https://www.cnblogs.com/xiaoying1245970347/p/5226321.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值