码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode:Roman to Integer

时间:2016-02-29 09:24:40      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

 

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 };

 

LeetCode:Roman to Integer

标签:

原文地址:http://www.cnblogs.com/xiaoying1245970347/p/5226321.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!