-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path13-roman-to-integer.php
166 lines (149 loc) · 3.75 KB
/
13-roman-to-integer.php
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
// 13. Roman to Integer
// https://leetcode.com/problems/roman-to-integer/
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 12 ms, faster than 95.54% of PHP online submissions
// for Roman to Integer.
// Memory Usage: 14.8 MB, less than 52.87% of PHP online submissions
// for Roman to Integer.
// class Solution
// {
// /**
// * @param String $s
// * @return Integer
// */
// function romanToInt($s)
// {
// $sum = 0;
// $dict = [
// 'I' => 1,
// 'IV' => 4,
// 'V' => 5,
// 'IX' => 9,
// 'X' => 10,
// 'XL' => 40,
// 'L' => 50,
// 'XC' => 90,
// 'C' => 100,
// 'CD' => 400,
// 'D' => 500,
// 'CM' => 900,
// 'M' => 1000,
// ];
// for ($i = strlen($s) - 1; 0 <= $i; $i--) {
// list($c, $n) = [$s[$i], $s[$i - 1] . $s[$i]];
// if (0 < $i && isset($dict[$n])) {
// $sum += $dict[$n];
// $i--;
// continue;
// }
// $sum += $dict[$c];
// }
// return $sum;
// }
// }
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 24 ms, faster than 80.67% of PHP online submissions
// for Roman to Integer.
// Memory Usage: 14.8 MB, less than 70.69% of PHP online submissions
// for Roman to Integer.
// class Solution
// {
// /**
// * @param String $s
// * @return Integer
// */
// function romanToInt($s)
// {
// foreach ([
// 'IV' => 4,
// 'IX' => 9,
// 'XL' => 40,
// 'XC' => 90,
// 'CD' => 400,
// 'CM' => 900,
// 'I' => 1,
// 'V' => 5,
// 'X' => 10,
// 'L' => 50,
// 'C' => 100,
// 'D' => 500,
// 'M' => 1000,
// ] as $r => $a) $s = str_replace($r, "$a,", $s);
// return array_sum(explode(',', $s));
// }
// }
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 40 ms, faster than 33.83% of PHP online submissions
// for Roman to Integer.
// Memory Usage: 14.8 MB, less than 72.99% of PHP online submissions
// for Roman to Integer.
class Solution
{
/**
* @param String $s
* @return Integer
*/
function romanToInt($s)
{
foreach ([
'IV' => 4,
'IX' => 9,
'XL' => 40,
'XC' => 90,
'CD' => 400,
'CM' => 900,
'I' => 1,
'V' => 5,
'X' => 10,
'L' => 50,
'C' => 100,
'D' => 500,
'M' => 1000,
] as $r => $a) $s = str_replace($r, "{$a}+", $s);
return eval("return {$s}0;");
}
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
$tests = [
[
'input' => 'III',
'expected' => 3,
],
[
'input' => 'IV',
'expected' => 4,
],
[
'input' => 'IX',
'expected' => 9,
],
[
'input' => 'LVIII',
'expected' => 58,
],
[
'input' => 'MCMXCIV',
'expected' => 1994,
],
[
'input' => 'MCDLXXVI',
'expected' => 1476,
],
[
'input' => 'MMMCDXC',
'expected' => 3490,
],
];
$s = new Solution();
foreach ($tests as $test) {
extract($test);
$output = $s->romanToInt($input);
if ($expected === $output) {
echo "✅ {$input}<br>";
} else {
echo "🔴 {$input}";
echo "<i>Expected <b>{$expected}</b>, but got <b>{$output}</b></i>";
}
echo '<hr>';
}