13. Roman to Integer C++

时间:2022-11-28 23:51:32

直接for循环,并且判断是否出现IV等情况

int which(char ch)
{
if(ch == 'I')
return ;
else if(ch == 'V')
return ;
else if(ch == 'X')
return ;
else if(ch == 'L')
return ;
else if(ch == 'C')
return ;
else if(ch == 'D')
return ;
else return ;
} class Solution {
public:
int romanToInt(string s) {
int flag = ;
int ans = ;
for(int i=; i<s.size(); i++)
{
if(i != s.size()- && which(s.at(i)) < which(s.at(i+)))
{
flag = ;
}
if(flag)
{
ans -= which(s.at(i));
flag = ;
}
else
ans += which(s.at(i));
}
cout << ans;
return ans;
}
};

13. Roman to Integer C++