-
Notifications
You must be signed in to change notification settings - Fork 0
/
Week1_Q6.cpp
43 lines (43 loc) · 1.05 KB
/
Week1_Q6.cpp
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
42
43
class Solution {
public:
int romanToInt(string s) {
int n = s.size();
vector<int> cnt(n, 0);
for(int i = 0; i < n; i++){
switch(s[i]){
case 'I':
cnt[i] = 1;
break;
case 'V':
cnt[i] = 5;
break;
case 'X':
cnt[i] = 10;
break;
case 'L':
cnt[i] = 50;
break;
case 'C':
cnt[i] = 100;
break;
case 'D':
cnt[i] = 500;
break;
case 'M':
cnt[i] = 1000;
break;
}
}
int sum = 0;
for(int i = 0; i < n - 1; i++) {
if(cnt[i] >= cnt[i+1]){
sum += cnt[i];
}
else {
sum -= cnt[i];
}
}
sum += cnt[n-1];
return sum;
}
};