13. 罗马数字转整数
Posted yuhong1103
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了13. 罗马数字转整数相关的知识,希望对你有一定的参考价值。
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. 罗马数字转整数的主要内容,如果未能解决你的问题,请参考以下文章