-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13.roman-to-integer.cpp
More file actions
50 lines (46 loc) · 1.19 KB
/
13.roman-to-integer.cpp
File metadata and controls
50 lines (46 loc) · 1.19 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
#include "testharness.h"
#include <cstdlib>
#include <string>
#include <iostream>
using namespace std;
class Solution {
public:
int romanToInt(string s) {
size_t size = s.size();
if (size == 0) return 0;
int num = 0;
size_t i = 0;
while (i < size) {
switch (s[i]) {
case 'M':
num += 1000;
break;
case 'D':
num += 500;
break;
case 'C':
num += i < size - 1 && (s[i+1] == 'D' || s[i+1] == 'M') ? -100 : 100;
break;
case 'L':
num += 50;
break;
case 'X':
num += i < size - 1 && (s[i+1] == 'L' || s[i+1] == 'C') ? -10 : 10;
break;
case 'V':
num += 5;
break;
case 'I':
num += i < size - 1 && (s[i+1] == 'V' || s[i+1] == 'X') ? -1 : 1;
break;
}
i++;
}
return num;
}
};
TEST(Solution, test) {
ASSERT_EQ(2, romanToInt("II"));
ASSERT_EQ(9, romanToInt("IX"));
ASSERT_EQ(3999, romanToInt("MMMCMXCIX"));
}