-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13.roman-to-integer.cpp
More file actions
42 lines (39 loc) · 937 Bytes
/
Copy path13.roman-to-integer.cpp
File metadata and controls
42 lines (39 loc) · 937 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
* @lc app=leetcode id=13 lang=cpp
*
* [13] Roman to Integer
*/
// @lc code=start
#include "bits/stdc++.h"
using namespace std;
class Solution {
public:
int romanToInt(string s) {
// use map to store key-value
std::unordered_map<char, int> myMap = {
{'I', 1},
{'V', 5},
{'X', 10},
{'L', 50},
{'C', 100},
{'D', 500},
{'M', 1000}
};
int n = s.size();
int i = -1;
int res = 0;
int preVal = 0;
while (++i < n)
{
// only no-decrease, plus value
if (i == n - 1 || myMap[s[i]] >= myMap[s[i + 1]]) {
res += myMap[s[i]] - preVal;
preVal = 0;
} else { // or substract the val, use preVal to accumulate
preVal += myMap[s[i]];
}
}
return res;
}
};
// @lc code=end