/
13.Roman to Integer.cpp
54 lines (47 loc) · 1.05 KB
/
13.Roman to Integer.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
44
45
46
47
48
49
50
51
52
#include<iostream>
#include<algorithm>
#include<string>
#include<unordered_set>
#include<unordered_map>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
int romanToInt(string s) {
vector<int> vec;
int res = 0;
for(int i = 0;i < s.length(); i++ ){
switch(s[i]){
case 'I': vec.push_back(1);break;
case 'V': vec.push_back(5);break;
case 'X': vec.push_back(10);break;
case 'L': vec.push_back(50);break;
case 'C': vec.push_back(100);break;
case 'D': vec.push_back(500);break;
case 'M': vec.push_back(1000);break;
}
}
for(int i = 0;i < s.size(); i++){
if(i + 1 < vec.size() && vec[i] < vec[i+1])
res -= vec[i];
else
res += vec[i];
}
return res;
}
void test(){
cout<<romanToInt("")<<endl;
cout<<romanToInt("XXXIV")<<endl;
cout<<romanToInt("MDCLXVI")<<endl;
cout<<romanToInt("MDCCCLXXXVIII")<<endl;
}
};
int main(){
Solution A;
A.test();
system("pause");
}