Skip to content

Commit 5265b72

Browse files
committed
Add LeetCode-13 Roman To Integer
1 parent c433f46 commit 5265b72

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed

__tests__/13-roman-to-integer.spec.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { romanToInt } from '../src/leet-code/1-easy/13-roman-to-integer';
2+
3+
describe(romanToInt.name, () => {
4+
it('Should work', () => {
5+
interface TestCase {
6+
input: string;
7+
expectedOutput: number;
8+
};
9+
10+
const testCases: TestCase[] = [
11+
{ input: 'I', expectedOutput: 1 },
12+
{ input: 'II', expectedOutput: 2 },
13+
{ input: 'III', expectedOutput: 3 },
14+
{ input: 'IV', expectedOutput: 4 },
15+
{ input: 'V', expectedOutput: 5 },
16+
{ input: 'VI', expectedOutput: 6 },
17+
{ input: 'VII', expectedOutput: 7 },
18+
{ input: 'VIII', expectedOutput: 8 },
19+
{ input: 'IX', expectedOutput: 9 },
20+
{ input: 'X', expectedOutput: 10 },
21+
{ input: 'XI', expectedOutput: 11 },
22+
{ input: 'XII', expectedOutput: 12 },
23+
{ input: 'XIII', expectedOutput: 13 },
24+
{ input: 'XIV', expectedOutput: 14 },
25+
{ input: 'XV', expectedOutput: 15 },
26+
{ input: 'XVI', expectedOutput: 16 },
27+
{ input: 'XVII', expectedOutput: 17 },
28+
{ input: 'XVIII', expectedOutput: 18 },
29+
{ input: 'XIX', expectedOutput: 19 },
30+
{ input: 'XX', expectedOutput: 20 },
31+
{ input: 'XXX', expectedOutput: 30 },
32+
{ input: 'XL', expectedOutput: 40 },
33+
{ input: 'L', expectedOutput: 50 },
34+
{ input: 'LX', expectedOutput: 60 },
35+
{ input: 'LXX', expectedOutput: 70 },
36+
{ input: 'LXXX', expectedOutput: 80 },
37+
{ input: 'XC', expectedOutput: 90 },
38+
{ input: 'C', expectedOutput: 100 },
39+
{ input: 'CC', expectedOutput: 200 },
40+
{ input: 'CCC', expectedOutput: 300 },
41+
{ input: 'CD', expectedOutput: 400 },
42+
{ input: 'D', expectedOutput: 500 },
43+
{ input: 'DC', expectedOutput: 600 },
44+
{ input: 'DCC', expectedOutput: 700 },
45+
{ input: 'DCCC', expectedOutput: 800 },
46+
{ input: 'CM', expectedOutput: 900 },
47+
{ input: 'M', expectedOutput: 1000 },
48+
{ input: 'MM', expectedOutput: 2000 },
49+
{ input: 'MMM', expectedOutput: 3000 },
50+
{ input: 'MMMM', expectedOutput: 4000 },
51+
{ input: 'MMMMM', expectedOutput: 5000 },
52+
{ input: 'MCMXCIX', expectedOutput: 1999 },
53+
];
54+
testCases.forEach(testCase => {
55+
const { input, expectedOutput } = testCase;
56+
expect(romanToInt(input)).toEqual(expectedOutput);
57+
});
58+
});
59+
});
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
Roman numerals are represented by seven different symbols: `I`, `V`, `X`, `L`, `C`, `D` and `M`.
3+
4+
Symbol Value
5+
I 1
6+
V 5
7+
X 10
8+
L 50
9+
C 100
10+
D 500
11+
M 1000
12+
For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.
13+
14+
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
15+
16+
I can be placed before V (5) and X (10) to make 4 and 9.
17+
X can be placed before L (50) and C (100) to make 40 and 90.
18+
C can be placed before D (500) and M (1000) to make 400 and 900.
19+
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
20+
21+
**Example 1**:
22+
23+
**Input**: "III"
24+
**Output**: 3
25+
26+
**Example 2:**
27+
28+
**Input**: "IV"
29+
**Output**: 4
30+
31+
**Example 3:**
32+
33+
**Input**: "IX"
34+
**Output**: 9
35+
36+
**Example 4:**
37+
38+
**Input**: "LVIII"
39+
**Output**: 58
40+
**Explanation**: L = 50, V= 5, III = 3.
41+
42+
**Example 5:**
43+
44+
**Input**: "MCMXCIV"
45+
**Output**: 1994
46+
**Explanation**: M = 1000, CM = 900, XC = 90 and IV = 4.
47+
*/
48+
49+
type Action = { result: number, skipNext: boolean };
50+
type Transitions = { [next: string]: Action };
51+
type StateMachine = { [current: string]: Transitions };
52+
53+
export const stateMachine: StateMachine = {
54+
'I': {
55+
'V': { result: 4, skipNext: true },
56+
'X': { result: 9, skipNext: true },
57+
'.': { result: 1, skipNext: false },
58+
},
59+
'V': {
60+
'.': { result: 5, skipNext: false },
61+
},
62+
'X': {
63+
'L': { result: 40, skipNext: true },
64+
'C': { result: 90, skipNext: true },
65+
'.': { result: 10, skipNext: false },
66+
},
67+
'L': {
68+
'.': { result: 50, skipNext: false },
69+
},
70+
'C': {
71+
'D': { result: 400, skipNext: true },
72+
'M': { result: 900, skipNext: true },
73+
'.': { result: 100, skipNext: false },
74+
},
75+
'D': {
76+
'.': { result: 500, skipNext: false },
77+
},
78+
'M': {
79+
'.': { result: 1000, skipNext: false },
80+
},
81+
};
82+
83+
export const romanToInt = function(roman: string): number {
84+
if (!roman || !roman.length) throw new Error(`${roman} is not a Roman number.`);
85+
86+
const queue = roman.toUpperCase().split('');
87+
let result = 0;
88+
while (queue.length) {
89+
const current = queue.shift()!;
90+
const transitions = stateMachine[current];
91+
const next = queue[0];
92+
const transition = transitions[next] || transitions['.'];
93+
result += transition.result;
94+
if (transition.skipNext && queue.length)
95+
queue.shift();
96+
}
97+
return result;
98+
};

0 commit comments

Comments
 (0)