diff --git a/solution/0000-0099/0013.Roman to Integer/README.md b/solution/0000-0099/0013.Roman to Integer/README.md index da85ba58aaf09..8f46058741599 100644 --- a/solution/0000-0099/0013.Roman to Integer/README.md +++ b/solution/0000-0099/0013.Roman to Integer/README.md @@ -186,6 +186,28 @@ func romanToInt(s string) int { } ``` +### **PHP** + +```php +class Solution { + /** + * @param String $s + * @return Integer + */ + function romanToInt($s) { + $hashmap = array('I' => 1, 'V' => 5, 'X' => 10, 'L' => 50, 'C' => 100, 'D' => 500, 'M' => 1000); + $rs = 0; + for ($i = 0; $i < strlen($s); $i++) { + $left = $hashmap[$s[$i]]; + $right = $hashmap[$s[$i + 1]]; + if ($left >= $right) $rs += $left; + else $rs -= $left; + } + return $rs; + } +} +``` + ### **...** ``` diff --git a/solution/0000-0099/0013.Roman to Integer/README_EN.md b/solution/0000-0099/0013.Roman to Integer/README_EN.md index d93738282bc25..9894b2b3951e9 100644 --- a/solution/0000-0099/0013.Roman to Integer/README_EN.md +++ b/solution/0000-0099/0013.Roman to Integer/README_EN.md @@ -164,6 +164,28 @@ func romanToInt(s string) int { } ``` +### **PHP** + +```php +class Solution { + /** + * @param String $s + * @return Integer + */ + function romanToInt($s) { + $hashmap = array('I' => 1, 'V' => 5, 'X' => 10, 'L' => 50, 'C' => 100, 'D' => 500, 'M' => 1000); + $rs = 0; + for ($i = 0; $i < strlen($s); $i++) { + $left = $hashmap[$s[$i]]; + $right = $hashmap[$s[$i + 1]]; + if ($left >= $right) $rs += $left; + else $rs -= $left; + } + return $rs; + } +} +``` + ### **...** ``` diff --git a/solution/0000-0099/0013.Roman to Integer/Solution.php b/solution/0000-0099/0013.Roman to Integer/Solution.php new file mode 100644 index 0000000000000..4fe99943ef597 --- /dev/null +++ b/solution/0000-0099/0013.Roman to Integer/Solution.php @@ -0,0 +1,17 @@ +class Solution { + /** + * @param String $s + * @return Integer + */ + function romanToInt($s) { + $hashmap = array('I' => 1, 'V' => 5, 'X' => 10, 'L' => 50, 'C' => 100, 'D' => 500, 'M' => 1000); + $rs = 0; + for ($i = 0; $i < strlen($s); $i++) { + $left = $hashmap[$s[$i]]; + $right = $hashmap[$s[$i + 1]]; + if ($left >= $right) $rs += $left; + else $rs -= $left; + } + return $rs; + } +} \ No newline at end of file