Skip to content

Commit b5d8d5f

Browse files
committed
Implement roman-to-integer
name: roman-to-integer url: https://leetcode.com/problems/roman-to-integer difficulty: easy time: 4ms time-rank: 92.61% time-complexity: O(N) space: 6.3MB space-rank: 80.43% space-complexity: O(1) Fixes #226 Signed-off-by: Christopher Friedt <[email protected]>
1 parent 9c8a2cf commit b5d8d5f

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed

roman-to-integer-test.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2018 Christopher Friedt
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
#include <gtest/gtest.h>
26+
27+
#include "roman-to-integer.cpp"
28+
29+
TEST(RomanToInteger, II) { EXPECT_EQ(2, Solution().romanToInt("II")); }
30+
TEST(RomanToInteger, XII) { EXPECT_EQ(12, Solution().romanToInt("XII")); }
31+
TEST(RomanToInteger, XXVII) { EXPECT_EQ(27, Solution().romanToInt("XXVII")); }
32+
TEST(RomanToInteger, IV) { EXPECT_EQ(4, Solution().romanToInt("IV")); }
33+
TEST(RomanToInteger, IX) { EXPECT_EQ(9, Solution().romanToInt("IX")); }
34+
TEST(RomanToInteger, DCXXI) { EXPECT_EQ(621, Solution().romanToInt("DCXXI")); }
35+
TEST(RomanToInteger, MCMXCIV) {
36+
EXPECT_EQ(1994, Solution().romanToInt("MCMXCIV"));
37+
}

roman-to-integer.cpp

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2018 Christopher Friedt
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
#include <string>
26+
#include <unordered_map>
27+
28+
using namespace std;
29+
30+
// https://leetcode.com/problems/roman-to-integer/
31+
32+
static inline int table1(char c) {
33+
switch (c) {
34+
case 'I':
35+
return 1;
36+
case 'V':
37+
return 5;
38+
case 'X':
39+
return 10;
40+
case 'L':
41+
return 50;
42+
case 'C':
43+
return 100;
44+
case 'D':
45+
return 500;
46+
case 'M':
47+
return 1000;
48+
default:
49+
return 0;
50+
}
51+
}
52+
53+
class Solution {
54+
public:
55+
int romanToInt(string s) {
56+
int sum = 0;
57+
for (int N = s.size(), i = N - 1; i >= 0;) {
58+
if (N == 0) {
59+
break;
60+
}
61+
62+
char c2 = s[i];
63+
if (N == 1) {
64+
sum += table1(c2);
65+
break;
66+
}
67+
68+
char c1 = s[i - 1];
69+
if (false) {
70+
} else if (c1 == 'I' && c2 == 'V') {
71+
sum += 4;
72+
N -= 2;
73+
i -= 2;
74+
continue;
75+
} else if (c1 == 'I' && c2 == 'X') {
76+
sum += 9;
77+
N -= 2;
78+
i -= 2;
79+
continue;
80+
} else if (c1 == 'X' && c2 == 'L') {
81+
sum += 40;
82+
N -= 2;
83+
i -= 2;
84+
continue;
85+
} else if (c1 == 'X' && c2 == 'C') {
86+
sum += 90;
87+
N -= 2;
88+
i -= 2;
89+
continue;
90+
} else if (c1 == 'C' && c2 == 'D') {
91+
sum += 400;
92+
N -= 2;
93+
i -= 2;
94+
continue;
95+
} else if (c1 == 'C' && c2 == 'M') {
96+
sum += 900;
97+
N -= 2;
98+
i -= 2;
99+
continue;
100+
}
101+
102+
sum += table1(c2);
103+
--i;
104+
--N;
105+
}
106+
107+
return sum;
108+
}
109+
};

0 commit comments

Comments
 (0)