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

13. 罗马数字转整数

时间:2020-03-15 18:58:46      阅读:59      评论:0      收藏:0      [点我收藏+]

标签:ant   nbsp   bsp   罗马   string   数字   substr   back   color   

 1 class Solution 
 2 {
 3 public:
 4     int a[13] = {1,4,5,9,10,40,50,90,100,400,500,900,1000};
 5     vector<string> b = {"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"};
 6 public:
 7     int romanToInt(string s) 
 8     {
 9         int res = 0;
10         for(int i = 0;i < s.size();)
11         {
12             string str = s.substr(i,2);
13             auto it = find(b.begin(),b.end(),str);
14             if(it != b.end()) 
15             {
16                 res += a[it - b.begin()];
17                 i += 2;
18             }
19             else
20             {
21                 string str;
22                 str.push_back(s[i]);
23                 auto iter = find(b.begin(),b.end(),str);
24                 res += a[(iter - b.begin())];
25                 i++;
26             }
27         }
28         return res;
29     }
30 };

 

13. 罗马数字转整数

标签:ant   nbsp   bsp   罗马   string   数字   substr   back   color   

原文地址:https://www.cnblogs.com/yuhong1103/p/12499130.html

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