Straightforward C++ lookup implementation

• ``````class Solution {
public:
int romanToInt(string s) {
if (!s.size())
return 0;

std::unordered_map<char,int> ht = {
{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000}
};

int val = ht[s[0]];
// Compare the current letter with the previous letter. If the current letter represents a smaller or equal value,
// add. If the current letter represents a greater value, subtract the previous smaller value two times because to
for (int i = 1; i < s.size(); i++) {
if (ht[s[i]] > ht[s[i-1]])
val = val + ht[s[i]] - 2*ht[s[i-1]];
else
val += ht[s[i]];
}
return val;

}
};``````

• ``````int romanToInt(string s) {
int x,y,dec,a[26],j,flag=0;
for(int i=0;i<26;i++)
a[i]=0;
a[3]=100;
a[4]=500;
a[9]=1;
a[12]=50;
a[13]=1000;
a[22]=5;
a[24]=10;
for(int i=0;s[i]!='\0';++i)
{
j=s[i]-64;
if(flag==0)
{
dec=a[j];
flag=1;
}
else
{
y=a[j];
x=a[s[i-1]-64];
if(y>x)
dec=(dec-x)+(y-x);

else
dec=dec+y;
}
}
return dec;
}
``````

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