diff --git a/solution/0000-0099/0004.Median of Two Sorted Arrays/README.md b/solution/0000-0099/0004.Median of Two Sorted Arrays/README.md index 839cecea32014..3cbc61669a55c 100644 --- a/solution/0000-0099/0004.Median of Two Sorted Arrays/README.md +++ b/solution/0000-0099/0004.Median of Two Sorted Arrays/README.md @@ -271,6 +271,28 @@ public class Solution { } ``` +```php +class Solution { + /** + * @param int[] $nums1 + * @param int[] $nums2 + * @return float + */ + + function findMedianSortedArrays($nums1, $nums2) { + $arr = array_merge($nums1, $nums2); + sort($arr); + $cnt_arr = count($arr); + + if ($cnt_arr % 2) { + return $arr[$cnt_arr / 2]; + } else { + return ($arr[intdiv($cnt_arr, 2) - 1] + $arr[intdiv($cnt_arr, 2)]) / 2; + } + } +} +``` + ```nim import std/[algorithm, sequtils] diff --git a/solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md b/solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md index 044bb180b05d3..4d7e34f073df0 100644 --- a/solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md +++ b/solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md @@ -265,6 +265,28 @@ public class Solution { } ``` +```php +class Solution { + /** + * @param int[] $nums1 + * @param int[] $nums2 + * @return float + */ + + function findMedianSortedArrays($nums1, $nums2) { + $arr = array_merge($nums1, $nums2); + sort($arr); + $cnt_arr = count($arr); + + if ($cnt_arr % 2) { + return $arr[$cnt_arr / 2]; + } else { + return ($arr[intdiv($cnt_arr, 2) - 1] + $arr[intdiv($cnt_arr, 2)]) / 2; + } + } +} +``` + ```nim import std/[algorithm, sequtils] diff --git a/solution/0000-0099/0004.Median of Two Sorted Arrays/Solution.php b/solution/0000-0099/0004.Median of Two Sorted Arrays/Solution.php new file mode 100644 index 0000000000000..60ea3f1c9a029 --- /dev/null +++ b/solution/0000-0099/0004.Median of Two Sorted Arrays/Solution.php @@ -0,0 +1,19 @@ +class Solution { + /** + * @param int[] $nums1 + * @param int[] $nums2 + * @return float + */ + + function findMedianSortedArrays($nums1, $nums2) { + $arr = array_merge($nums1, $nums2); + sort($arr); + $cnt_arr = count($arr); + + if ($cnt_arr % 2) { + return $arr[$cnt_arr / 2]; + } else { + return ($arr[intdiv($cnt_arr, 2) - 1] + $arr[intdiv($cnt_arr, 2)]) / 2; + } + } +} diff --git a/solution/0000-0099/0005.Longest Palindromic Substring/README.md b/solution/0000-0099/0005.Longest Palindromic Substring/README.md index 1922159a8e432..4febbf23c25b0 100644 --- a/solution/0000-0099/0005.Longest Palindromic Substring/README.md +++ b/solution/0000-0099/0005.Longest Palindromic Substring/README.md @@ -410,6 +410,42 @@ impl Solution { } ``` +```php +class Solution { + /** + * @param string $s + * @return string + */ + function longestPalindrome($s) { + $start = 0; + $maxLength = 0; + + for ($i = 0; $i < strlen($s); $i++) { + $len1 = $this->expandFromCenter($s, $i, $i); + $len2 = $this->expandFromCenter($s, $i, $i + 1); + + $len = max($len1, $len2); + + if ($len > $maxLength) { + $start = $i - intval(($len - 1) / 2); + $maxLength = $len; + } + } + + return substr($s, $start, $maxLength); + } + + function expandFromCenter($s, $left, $right) { + while ($left >= 0 && $right < strlen($s) && $s[$left] === $s[$right]) { + $left--; + $right++; + } + + return $right - $left - 1; + } +} +``` + diff --git a/solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md b/solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md index ccf126a3e3591..7618bff4fb816 100644 --- a/solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md +++ b/solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md @@ -404,6 +404,42 @@ impl Solution { } ``` +```php +class Solution { + /** + * @param string $s + * @return string + */ + function longestPalindrome($s) { + $start = 0; + $maxLength = 0; + + for ($i = 0; $i < strlen($s); $i++) { + $len1 = $this->expandFromCenter($s, $i, $i); + $len2 = $this->expandFromCenter($s, $i, $i + 1); + + $len = max($len1, $len2); + + if ($len > $maxLength) { + $start = $i - intval(($len - 1) / 2); + $maxLength = $len; + } + } + + return substr($s, $start, $maxLength); + } + + function expandFromCenter($s, $left, $right) { + while ($left >= 0 && $right < strlen($s) && $s[$left] === $s[$right]) { + $left--; + $right++; + } + + return $right - $left - 1; + } +} +``` + diff --git a/solution/0000-0099/0005.Longest Palindromic Substring/Solution.php b/solution/0000-0099/0005.Longest Palindromic Substring/Solution.php new file mode 100644 index 0000000000000..854ef5be2cbe8 --- /dev/null +++ b/solution/0000-0099/0005.Longest Palindromic Substring/Solution.php @@ -0,0 +1,33 @@ +class Solution { + /** + * @param string $s + * @return string + */ + function longestPalindrome($s) { + $start = 0; + $maxLength = 0; + + for ($i = 0; $i < strlen($s); $i++) { + $len1 = $this->expandFromCenter($s, $i, $i); + $len2 = $this->expandFromCenter($s, $i, $i + 1); + + $len = max($len1, $len2); + + if ($len > $maxLength) { + $start = $i - intval(($len - 1) / 2); + $maxLength = $len; + } + } + + return substr($s, $start, $maxLength); + } + + function expandFromCenter($s, $left, $right) { + while ($left >= 0 && $right < strlen($s) && $s[$left] === $s[$right]) { + $left--; + $right++; + } + + return $right - $left - 1; + } +} diff --git a/solution/0000-0099/0006.Zigzag Conversion/README.md b/solution/0000-0099/0006.Zigzag Conversion/README.md index 51157d9ea0ac2..d6759dabc9a9d 100644 --- a/solution/0000-0099/0006.Zigzag Conversion/README.md +++ b/solution/0000-0099/0006.Zigzag Conversion/README.md @@ -411,6 +411,38 @@ var convert = function (s, numRows) { }; ``` +```php +class Solution { + /** + * @param string $s + * @param int $numRows + * @return string + */ + + function convert($s, $numRows) { + if ($numRows == 1 || strlen($s) <= $numRows) { + return $s; + } + + $result = ''; + $cycleLength = 2 * $numRows - 2; + $n = strlen($s); + + for ($i = 0; $i < $numRows; $i++) { + for ($j = 0; $j + $i < $n; $j += $cycleLength) { + $result .= $s[$j + $i]; + + if ($i != 0 && $i != $numRows - 1 && $j + $cycleLength - $i < $n) { + $result .= $s[$j + $cycleLength - $i]; + } + } + } + + return $result; + } +} +``` + diff --git a/solution/0000-0099/0006.Zigzag Conversion/README_EN.md b/solution/0000-0099/0006.Zigzag Conversion/README_EN.md index 21c753e25c056..c3e43cc7779a2 100644 --- a/solution/0000-0099/0006.Zigzag Conversion/README_EN.md +++ b/solution/0000-0099/0006.Zigzag Conversion/README_EN.md @@ -407,6 +407,38 @@ var convert = function (s, numRows) { }; ``` +```php +class Solution { + /** + * @param string $s + * @param int $numRows + * @return string + */ + + function convert($s, $numRows) { + if ($numRows == 1 || strlen($s) <= $numRows) { + return $s; + } + + $result = ''; + $cycleLength = 2 * $numRows - 2; + $n = strlen($s); + + for ($i = 0; $i < $numRows; $i++) { + for ($j = 0; $j + $i < $n; $j += $cycleLength) { + $result .= $s[$j + $i]; + + if ($i != 0 && $i != $numRows - 1 && $j + $cycleLength - $i < $n) { + $result .= $s[$j + $cycleLength - $i]; + } + } + } + + return $result; + } +} +``` + diff --git a/solution/0000-0099/0006.Zigzag Conversion/Solution.php b/solution/0000-0099/0006.Zigzag Conversion/Solution.php new file mode 100644 index 0000000000000..4eb4ee9795308 --- /dev/null +++ b/solution/0000-0099/0006.Zigzag Conversion/Solution.php @@ -0,0 +1,29 @@ +class Solution { + /** + * @param string $s + * @param int $numRows + * @return string + */ + + function convert($s, $numRows) { + if ($numRows == 1 || strlen($s) <= $numRows) { + return $s; + } + + $result = ''; + $cycleLength = 2 * $numRows - 2; + $n = strlen($s); + + for ($i = 0; $i < $numRows; $i++) { + for ($j = 0; $j + $i < $n; $j += $cycleLength) { + $result .= $s[$j + $i]; + + if ($i != 0 && $i != $numRows - 1 && $j + $cycleLength - $i < $n) { + $result .= $s[$j + $cycleLength - $i]; + } + } + } + + return $result; + } +} diff --git a/solution/0000-0099/0007.Reverse Integer/README.md b/solution/0000-0099/0007.Reverse Integer/README.md index 1c0474905adb1..caaa6795a595d 100644 --- a/solution/0000-0099/0007.Reverse Integer/README.md +++ b/solution/0000-0099/0007.Reverse Integer/README.md @@ -193,6 +193,36 @@ int reverse(int x) { } ``` +```php +class Solution { + /** + * @param int $x + * @return int + */ + + function reverse($x) { + $isNegative = $x < 0; + $x = abs($x); + + $reversed = 0; + + while ($x > 0) { + $reversed = $reversed * 10 + ($x % 10); + $x = (int) ($x / 10); + } + + if ($isNegative) { + $reversed *= -1; + } + if ($reversed < -pow(2, 31) || $reversed > pow(2, 31) - 1) { + return 0; + } + + return $reversed; + } +} +``` + diff --git a/solution/0000-0099/0007.Reverse Integer/README_EN.md b/solution/0000-0099/0007.Reverse Integer/README_EN.md index 7512badd0522a..f6d980486ee23 100644 --- a/solution/0000-0099/0007.Reverse Integer/README_EN.md +++ b/solution/0000-0099/0007.Reverse Integer/README_EN.md @@ -181,6 +181,36 @@ int reverse(int x) { } ``` +```php +class Solution { + /** + * @param int $x + * @return int + */ + + function reverse($x) { + $isNegative = $x < 0; + $x = abs($x); + + $reversed = 0; + + while ($x > 0) { + $reversed = $reversed * 10 + ($x % 10); + $x = (int) ($x / 10); + } + + if ($isNegative) { + $reversed *= -1; + } + if ($reversed < -pow(2, 31) || $reversed > pow(2, 31) - 1) { + return 0; + } + + return $reversed; + } +} +``` + diff --git a/solution/0000-0099/0007.Reverse Integer/Solution.php b/solution/0000-0099/0007.Reverse Integer/Solution.php new file mode 100644 index 0000000000000..ed969b9ba5adf --- /dev/null +++ b/solution/0000-0099/0007.Reverse Integer/Solution.php @@ -0,0 +1,27 @@ +class Solution { + /** + * @param int $x + * @return int + */ + + function reverse($x) { + $isNegative = $x < 0; + $x = abs($x); + + $reversed = 0; + + while ($x > 0) { + $reversed = $reversed * 10 + ($x % 10); + $x = (int) ($x / 10); + } + + if ($isNegative) { + $reversed *= -1; + } + if ($reversed < -pow(2, 31) || $reversed > pow(2, 31) - 1) { + return 0; + } + + return $reversed; + } +} diff --git a/solution/0000-0099/0008.String to Integer (atoi)/README.md b/solution/0000-0099/0008.String to Integer (atoi)/README.md index 510f85674addd..b6a7666ce69f8 100644 --- a/solution/0000-0099/0008.String to Integer (atoi)/README.md +++ b/solution/0000-0099/0008.String to Integer (atoi)/README.md @@ -275,6 +275,26 @@ public partial class Solution } ``` +```php +class Solution { + /** + * @param string $s + * @return int + */ + + function myAtoi($s) { + $s = str_replace('e', 'x', $s); + if (intval($s) < pow(-2, 31)) { + return -2147483648; + } + if (intval($s) > pow(2, 31) - 1) { + return 2147483647; + } + return intval($s); + } +} +``` + diff --git a/solution/0000-0099/0008.String to Integer (atoi)/README_EN.md b/solution/0000-0099/0008.String to Integer (atoi)/README_EN.md index a93f2c2e3866f..509b9fc3d0a6f 100644 --- a/solution/0000-0099/0008.String to Integer (atoi)/README_EN.md +++ b/solution/0000-0099/0008.String to Integer (atoi)/README_EN.md @@ -270,6 +270,26 @@ public partial class Solution } ``` +```php +class Solution { + /** + * @param string $s + * @return int + */ + + function myAtoi($s) { + $s = str_replace('e', 'x', $s); + if (intval($s) < pow(-2, 31)) { + return -2147483648; + } + if (intval($s) > pow(2, 31) - 1) { + return 2147483647; + } + return intval($s); + } +} +``` + diff --git a/solution/0000-0099/0008.String to Integer (atoi)/Solution.php b/solution/0000-0099/0008.String to Integer (atoi)/Solution.php new file mode 100644 index 0000000000000..c4943192194ce --- /dev/null +++ b/solution/0000-0099/0008.String to Integer (atoi)/Solution.php @@ -0,0 +1,17 @@ +class Solution { + /** + * @param string $s + * @return int + */ + + function myAtoi($s) { + $s = str_replace('e', 'x', $s); + if (intval($s) < pow(-2, 31)) { + return -2147483648; + } + if (intval($s) > pow(2, 31) - 1) { + return 2147483647; + } + return intval($s); + } +} diff --git a/solution/0000-0099/0009.Palindrome Number/README.md b/solution/0000-0099/0009.Palindrome Number/README.md index 8ac7f651bd3dc..64912341cadaf 100644 --- a/solution/0000-0099/0009.Palindrome Number/README.md +++ b/solution/0000-0099/0009.Palindrome Number/README.md @@ -211,6 +211,21 @@ impl Solution { } ``` +```php +class Solution { + /** + * @param int $x + * @return boolean + */ + + function isPalindrome($x) { + $str = (string) $x; + $str_reverse = strrev($str); + return $str === $str_reverse; + } +} +``` + diff --git a/solution/0000-0099/0009.Palindrome Number/README_EN.md b/solution/0000-0099/0009.Palindrome Number/README_EN.md index 86cb617af9bea..652c98e07d461 100644 --- a/solution/0000-0099/0009.Palindrome Number/README_EN.md +++ b/solution/0000-0099/0009.Palindrome Number/README_EN.md @@ -201,6 +201,21 @@ impl Solution { } ``` +```php +class Solution { + /** + * @param int $x + * @return boolean + */ + + function isPalindrome($x) { + $str = (string) $x; + $str_reverse = strrev($str); + return $str === $str_reverse; + } +} +``` + diff --git a/solution/0000-0099/0009.Palindrome Number/Solution.php b/solution/0000-0099/0009.Palindrome Number/Solution.php new file mode 100644 index 0000000000000..9fd8c6654ec95 --- /dev/null +++ b/solution/0000-0099/0009.Palindrome Number/Solution.php @@ -0,0 +1,12 @@ +class Solution { + /** + * @param int $x + * @return boolean + */ + + function isPalindrome($x) { + $str = (string) $x; + $str_reverse = strrev($str); + return $str === $str_reverse; + } +} diff --git a/solution/0000-0099/0010.Regular Expression Matching/README.md b/solution/0000-0099/0010.Regular Expression Matching/README.md index d8da6408e5118..cd840d24d6ed6 100644 --- a/solution/0000-0099/0010.Regular Expression Matching/README.md +++ b/solution/0000-0099/0010.Regular Expression Matching/README.md @@ -451,6 +451,47 @@ public class Solution { } ``` +```php +class Solution { + /** + * @param string $s + * @param string $p + * @return boolean + */ + + function isMatch($s, $p) { + $m = strlen($s); + $n = strlen($p); + + $dp = array_fill(0, $m + 1, array_fill(0, $n + 1, false)); + $dp[0][0] = true; + + for ($j = 1; $j <= $n; $j++) { + if ($p[$j - 1] == '*') { + $dp[0][$j] = $dp[0][$j - 2]; + } + } + + for ($i = 1; $i <= $m; $i++) { + for ($j = 1; $j <= $n; $j++) { + if ($p[$j - 1] == '.' || $p[$j - 1] == $s[$i - 1]) { + $dp[$i][$j] = $dp[$i - 1][$j - 1]; + } elseif ($p[$j - 1] == '*') { + $dp[$i][$j] = $dp[$i][$j - 2]; + if ($p[$j - 2] == '.' || $p[$j - 2] == $s[$i - 1]) { + $dp[$i][$j] = $dp[$i][$j] || $dp[$i - 1][$j]; + } + } else { + $dp[$i][$j] = false; + } + } + } + + return $dp[$m][$n]; + } +} +``` + diff --git a/solution/0000-0099/0010.Regular Expression Matching/README_EN.md b/solution/0000-0099/0010.Regular Expression Matching/README_EN.md index 22e6613f3ec73..2d435fcd70357 100644 --- a/solution/0000-0099/0010.Regular Expression Matching/README_EN.md +++ b/solution/0000-0099/0010.Regular Expression Matching/README_EN.md @@ -448,6 +448,47 @@ public class Solution { } ``` +```php +class Solution { + /** + * @param string $s + * @param string $p + * @return boolean + */ + + function isMatch($s, $p) { + $m = strlen($s); + $n = strlen($p); + + $dp = array_fill(0, $m + 1, array_fill(0, $n + 1, false)); + $dp[0][0] = true; + + for ($j = 1; $j <= $n; $j++) { + if ($p[$j - 1] == '*') { + $dp[0][$j] = $dp[0][$j - 2]; + } + } + + for ($i = 1; $i <= $m; $i++) { + for ($j = 1; $j <= $n; $j++) { + if ($p[$j - 1] == '.' || $p[$j - 1] == $s[$i - 1]) { + $dp[$i][$j] = $dp[$i - 1][$j - 1]; + } elseif ($p[$j - 1] == '*') { + $dp[$i][$j] = $dp[$i][$j - 2]; + if ($p[$j - 2] == '.' || $p[$j - 2] == $s[$i - 1]) { + $dp[$i][$j] = $dp[$i][$j] || $dp[$i - 1][$j]; + } + } else { + $dp[$i][$j] = false; + } + } + } + + return $dp[$m][$n]; + } +} +``` + diff --git a/solution/0000-0099/0010.Regular Expression Matching/Solution.php b/solution/0000-0099/0010.Regular Expression Matching/Solution.php new file mode 100644 index 0000000000000..85a7f34fe75da --- /dev/null +++ b/solution/0000-0099/0010.Regular Expression Matching/Solution.php @@ -0,0 +1,38 @@ +class Solution { + /** + * @param string $s + * @param string $p + * @return boolean + */ + + function isMatch($s, $p) { + $m = strlen($s); + $n = strlen($p); + + $dp = array_fill(0, $m + 1, array_fill(0, $n + 1, false)); + $dp[0][0] = true; + + for ($j = 1; $j <= $n; $j++) { + if ($p[$j - 1] == '*') { + $dp[0][$j] = $dp[0][$j - 2]; + } + } + + for ($i = 1; $i <= $m; $i++) { + for ($j = 1; $j <= $n; $j++) { + if ($p[$j - 1] == '.' || $p[$j - 1] == $s[$i - 1]) { + $dp[$i][$j] = $dp[$i - 1][$j - 1]; + } elseif ($p[$j - 1] == '*') { + $dp[$i][$j] = $dp[$i][$j - 2]; + if ($p[$j - 2] == '.' || $p[$j - 2] == $s[$i - 1]) { + $dp[$i][$j] = $dp[$i][$j] || $dp[$i - 1][$j]; + } + } else { + $dp[$i][$j] = false; + } + } + } + + return $dp[$m][$n]; + } +}