Problem:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
遍历一次输入的字符串,如果不满足类似4或者9的就直接加相应的数,否则减去相应的数。其中对应如下”IVXLCDM“对应{1,5,10,50,100,500,1000}
class Solution { public:
int romanToInt(string s)
{
string refe = "IVXLCDM";
int val[] = {, , , , , , };
int result = ; for (int i = ; i < s.size(); i++)
{
for (int j = ; j < refe.length(); j++)
{
if (i+<s.size() && s[i] == refe[j] && ((j+<refe.size() && s[i+] == refe[j+]) || (j+<refe.size() && s[i+] == refe[j+])))
{result -= val[j];}
else if (s[i] == refe[j])
{result += val[j];}
}
}
return result;
}
};