#include<iostream>
using namespace std;
#include<unordered_map>
class Solution
{
public:
int romanToInt(string s)
{
int a = 0;
int b = s.length();
for (int i = 0; i < b; ++i)
{
int value = abb[s[i]];
if (i < b - 1 && value < abb[s[i + 1]])
{
a -= value;
}
else
{
a += value;
}
}
return a;
}
private:
unordered_map<char, int>abb = { //哈希容器
{'I' , 1},
{'V' , 5},
{'X' , 10},
{'L' , 50},
{'C' , 100},
{'D' , 500},
{'M' , 1000},
};
};
int main()
{
Solution aa;
aa.romanToInt("XXVII");
system("pause");
return 0;
}
13 罗马数字转换为整数
最新推荐文章于 2024-10-31 18:26:46 发布