# How can I make this any faster? c++

• I don't understand the speed thing my first solution was 32ms. I a test from the if statement out and it got slower, 88ms. I'm not sure how I could make this thing any faster.

First solution:

``````class Solution {
public:
int romanToInt(string s) {
int r(0);
for(int i = s.size() - 1; i >= 0; i--)
{
if(i > 0 && nrm[s[i]] > nrm[s[i-1]])
{
r += nrm[s[i]] - nrm[s[i-1]];
i--;
} else {
r += nrm[s[i]];
}
}
return r;
}
private:
unordered_map<char, int> nrm = {
{'I',1},
{'V',5},
{'X',10},
{'L',50},
{'C',100},
{'D',500},
{'M',1000}
};

};
``````

Second solution:

``````class Solution {
public:
int romanToInt(string s) {
int r(0);
for(int i = 0; i < s.size(); i++)
{
if(nrm[s[i]] < nrm[s[i+1]])
{
r += nrm[s[i+1]] - nrm[s[i]];
i++;
} else {
r += nrm[s[i]];
}
}
return r;
}
private:
unordered_map<char, int> nrm = {
{'I',1},
{'V',5},
{'X',10},
{'L',50},
{'C',100},
{'D',500},
{'M',1000}
};

};
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.