MCPcopy Create free account
hub / github.com/Vishruth-S/CompetitiveCode / Solution

Class Solution

LeetCode_problems/Roman to Integer/solution.cpp:78–103  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

76*/
77
78class Solution {
79public:
80 int romanToInt(string s) {
81 map <char, int> mp;
82 mp['I'] = 1;
83 mp['V'] = 5;
84 mp['X'] = 10;
85 mp['L'] = 50;
86 mp['C'] = 100;
87 mp['D'] = 500;
88 mp['M'] = 1000;
89
90 int ans = 0;
91 int prev = mp[s[0]];
92
93 for (int i = 1; i < s.size(); i++) {
94 int mult = 1;
95
96 if (prev < mp[s[i]]) mult = -1;
97 ans += (mult * prev);
98 prev = mp[s[i]];
99 }
100
101 return ans + prev;
102 }
103};

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected