标签: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 };
标签:ant nbsp bsp 罗马 string 数字 substr back color
原文地址:https://www.cnblogs.com/yuhong1103/p/12499130.html