s0013
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-11-03 16:09:44 +08:00
parent e865d369e6
commit 0df74464b7
3 changed files with 65 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
#include "s0013_roman_to_integer.hpp"
int Solution::romanToInt(string s) {
unordered_map<string, int> map;
map["I"] = 1;
map["V"] = 5;
map["X"] = 10;
map["L"] = 50;
map["C"] = 100;
map["D"] = 500;
map["M"] = 1000;
int val{0};
int len = s.length();
for (int i{0}; i < len; i++) {
if (i == len - 1) {
val += map.at(s.substr(i, 1));
} else {
if (map.at(s.substr(i, 1)) < map.at(s.substr(i + 1, 1))) {
val -= map.at(s.substr(i, 1));
} else {
val += map.at(s.substr(i, 1));
}
}
}
return val;
}