-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroman_to_integer.cpp
More file actions
75 lines (70 loc) · 1.76 KB
/
roman_to_integer.cpp
File metadata and controls
75 lines (70 loc) · 1.76 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
* =====================================================================================
*
* Filename: roman_to_integer.cpp
*
* Description: Given a roman numeral, convert it to an integer. Input is guaranteed
* to be within the range from 1 to 3999.
*
* Version: 1.0
* Created: 06/29/18 07:32:10
* Revision: none
* Compiler: gcc
*
* Author: Zhu Xianfeng (), xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <stdio.h>
#include <string>
#include <vector>
class Solution
{
public:
int romanToInt(const std::string& roman)
{
using sym_pair = std::pair<int, std::string>;
std::vector<sym_pair> symbols = {
{1, "I"},
{4, "IV"},
{5, "V"},
{9, "IX"},
{10, "X"},
{40, "XL"},
{50, "L"},
{90, "XC"},
{100, "C"},
{400, "CD"},
{500, "D"},
{900, "CM"},
{1000, "M"},
};
int num = 0;
size_t idx = 0;
for (auto iter = symbols.rbegin(); iter != symbols.rend(); iter++)
{
while (iter->second == roman.substr(idx, iter->second.length()))
{
num += iter->first;
idx += iter->second.length();
}
if (idx >= roman.length())
{
break;
}
}
return num;
}
};
int main(int argc, char* argv[])
{
std::string roman = "LVIII";
if (argc > 1)
{
roman = argv[1];
}
int num = Solution().romanToInt(roman);
printf("%s -> %d\n", roman.c_str(), num);
return 0;
}