Skip to content

Commit 9e43d40

Browse files
authored
feat: add php solution to lc problem: No.0013 (doocs#952)
1 parent 8d1b5e9 commit 9e43d40

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

solution/0000-0099/0013.Roman to Integer/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,28 @@ func romanToInt(s string) int {
186186
}
187187
```
188188

189+
### **PHP**
190+
191+
```php
192+
class Solution {
193+
/**
194+
* @param String $s
195+
* @return Integer
196+
*/
197+
function romanToInt($s) {
198+
$hashmap = array('I' => 1, 'V' => 5, 'X' => 10, 'L' => 50, 'C' => 100, 'D' => 500, 'M' => 1000);
199+
$rs = 0;
200+
for ($i = 0; $i < strlen($s); $i++) {
201+
$left = $hashmap[$s[$i]];
202+
$right = $hashmap[$s[$i + 1]];
203+
if ($left >= $right) $rs += $left;
204+
else $rs -= $left;
205+
}
206+
return $rs;
207+
}
208+
}
209+
```
210+
189211
### **...**
190212

191213
```

solution/0000-0099/0013.Roman to Integer/README_EN.md

+22
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,28 @@ func romanToInt(s string) int {
164164
}
165165
```
166166

167+
### **PHP**
168+
169+
```php
170+
class Solution {
171+
/**
172+
* @param String $s
173+
* @return Integer
174+
*/
175+
function romanToInt($s) {
176+
$hashmap = array('I' => 1, 'V' => 5, 'X' => 10, 'L' => 50, 'C' => 100, 'D' => 500, 'M' => 1000);
177+
$rs = 0;
178+
for ($i = 0; $i < strlen($s); $i++) {
179+
$left = $hashmap[$s[$i]];
180+
$right = $hashmap[$s[$i + 1]];
181+
if ($left >= $right) $rs += $left;
182+
else $rs -= $left;
183+
}
184+
return $rs;
185+
}
186+
}
187+
```
188+
167189
### **...**
168190

169191
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
/**
3+
* @param String $s
4+
* @return Integer
5+
*/
6+
function romanToInt($s) {
7+
$hashmap = array('I' => 1, 'V' => 5, 'X' => 10, 'L' => 50, 'C' => 100, 'D' => 500, 'M' => 1000);
8+
$rs = 0;
9+
for ($i = 0; $i < strlen($s); $i++) {
10+
$left = $hashmap[$s[$i]];
11+
$right = $hashmap[$s[$i + 1]];
12+
if ($left >= $right) $rs += $left;
13+
else $rs -= $left;
14+
}
15+
return $rs;
16+
}
17+
}

0 commit comments

Comments
 (0)