Fetch the repository succeeded.
class Solution
{
public:
int romanToInt(string s)
{
int map[26];
map['I'-'A'] = 1;
map['V'-'A'] = 5;
map['X'-'A'] = 10;
map['L'-'A'] = 50;
map['C'-'A'] = 100;
map['D'-'A'] = 500;
map['M'-'A'] = 1000;
int result = 0, n = s.size();
s.push_back(s[n-1]);
for(int i = 0; i < n; i++)
{
if(map[s[i]-'A'] >= map[s[i+1]-'A'])
result += map[s[i]-'A'];
else result -= map[s[i]-'A'];
}
return result;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。