【LeetCode-简单】13. 罗马数字转整数 - 模拟
作者:互联网
解法:
class Solution {
public:
map<char, int> roman = {
{'I', 1},
{'V', 5},
{'X', 10},
{'L', 50},
{'C', 100},
{'D', 500},
{'M', 1000}
};
int romanToInt(string s) {
int n = s.length();
int result = 0;
for(int i=0; i<n; i++){
int value = roman[s[i]];
if(i<n-1 && value<roman[s[i+1]]){
result -= value;
}else{
result +=value;
}
}
return result;
}
};
标签:13,int,roman,value,罗马数字,result,LeetCode 来源: https://blog.csdn.net/qq_43619058/article/details/123633206