Skip to content

Commit

Permalink
Create 49 Roman to Integer.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
skjha1 authored Jul 15, 2021
1 parent 6713382 commit 4eb7efa
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Leetcode/Easy/49 Roman to Integer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public:
int romanToInt(string s)
{
unordered_map<char, int> T = { { 'I' , 1 },
{ 'V' , 5 },
{ 'X' , 10 },
{ 'L' , 50 },
{ 'C' , 100 },
{ 'D' , 500 },
{ 'M' , 1000 } };

int sum = T[s.back()];
for (int i = s.length() - 2; i >= 0; --i)
{
if (T[s[i]] < T[s[i + 1]])
{
sum -= T[s[i]];
}
else
{
sum += T[s[i]];
}
}

return sum;
}
};

0 comments on commit 4eb7efa

Please sign in to comment.