-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13.cpp
More file actions
47 lines (42 loc) · 1.2 KB
/
13.cpp
File metadata and controls
47 lines (42 loc) · 1.2 KB
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
44
45
46
47
// Problem : 13. Roman to Integer
// Link : https://leetcode.com/problems/roman-to-integer/
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
#include <string>
using namespace std;
class Solution {
public:
int romanToInt(string s) {
char symbols[7] = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};
int values[7] = {1, 5, 10, 50, 100, 500, 1000};
int len = s.length();
int num;
int answer = 0;
for (int i = 0; i < len; i++) {
for (int j = 0; j < 7; j++) {
if (int(s.at(i)) == int(symbols[j])) {
if (i == 0) {
answer = answer + values[j];
num = values[j];
break;
}
if (num < values[j]) {
answer = answer - num;
answer = answer + values[j] - num;
}
else
answer = answer + values[j];
num = values[j];
break;
}
}
}
return answer;
}
};
int main() {
Solution ob;
cout << ob.romanToInt("MCMXCIV");
return 0;
}