首页 > 其他 > 详细

LeetCode:Roman to Integer

时间:2016-02-29 09:21:39      阅读:192      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!