diff --git a/solution/0000-0099/0041.First Missing Positive/README.md b/solution/0000-0099/0041.First Missing Positive/README.md index c917e66a0dae8..182ba25347d40 100644 --- a/solution/0000-0099/0041.First Missing Positive/README.md +++ b/solution/0000-0099/0041.First Missing Positive/README.md @@ -222,6 +222,40 @@ void swap(int* a, int* b) { } ``` +```php +class Solution { + /** + * @param integer[] $nums + * @return integer + */ + + function firstMissingPositive($nums) { + $n = count($nums); + + for ($i = 0; $i < $n; $i++) { + if ($nums[$i] <= 0) { + $nums[$i] = $n + 1; + } + } + + for ($i = 0; $i < $n; $i++) { + $num = abs($nums[$i]); + if ($num <= $n) { + $nums[$num - 1] = -abs($nums[$num - 1]); + } + } + + for ($i = 0; $i < $n; $i++) { + if ($nums[$i] > 0) { + return $i + 1; + } + } + + return $n + 1; + } +} +``` + <!-- tabs:end --> <!-- end --> diff --git a/solution/0000-0099/0041.First Missing Positive/README_EN.md b/solution/0000-0099/0041.First Missing Positive/README_EN.md index c12a00921e934..8ca39f30ddb04 100644 --- a/solution/0000-0099/0041.First Missing Positive/README_EN.md +++ b/solution/0000-0099/0041.First Missing Positive/README_EN.md @@ -222,6 +222,40 @@ void swap(int* a, int* b) { } ``` +```php +class Solution { + /** + * @param integer[] $nums + * @return integer + */ + + function firstMissingPositive($nums) { + $n = count($nums); + + for ($i = 0; $i < $n; $i++) { + if ($nums[$i] <= 0) { + $nums[$i] = $n + 1; + } + } + + for ($i = 0; $i < $n; $i++) { + $num = abs($nums[$i]); + if ($num <= $n) { + $nums[$num - 1] = -abs($nums[$num - 1]); + } + } + + for ($i = 0; $i < $n; $i++) { + if ($nums[$i] > 0) { + return $i + 1; + } + } + + return $n + 1; + } +} +``` + <!-- tabs:end --> <!-- end --> diff --git a/solution/0000-0099/0041.First Missing Positive/Solution.php b/solution/0000-0099/0041.First Missing Positive/Solution.php new file mode 100644 index 0000000000000..f3aa6b8576f47 --- /dev/null +++ b/solution/0000-0099/0041.First Missing Positive/Solution.php @@ -0,0 +1,31 @@ +class Solution { + /** + * @param integer[] $nums + * @return integer + */ + + function firstMissingPositive($nums) { + $n = count($nums); + + for ($i = 0; $i < $n; $i++) { + if ($nums[$i] <= 0) { + $nums[$i] = $n + 1; + } + } + + for ($i = 0; $i < $n; $i++) { + $num = abs($nums[$i]); + if ($num <= $n) { + $nums[$num - 1] = -abs($nums[$num - 1]); + } + } + + for ($i = 0; $i < $n; $i++) { + if ($nums[$i] > 0) { + return $i + 1; + } + } + + return $n + 1; + } +} diff --git a/solution/0000-0099/0042.Trapping Rain Water/README.md b/solution/0000-0099/0042.Trapping Rain Water/README.md index 19d06c9e3da41..630680d72b9de 100644 --- a/solution/0000-0099/0042.Trapping Rain Water/README.md +++ b/solution/0000-0099/0042.Trapping Rain Water/README.md @@ -187,6 +187,48 @@ public class Solution { } ``` +```php +class Solution { + /** + * @param integer[] $height + * @return integer + */ + + function trap($height) { + $n = count($height); + + if ($n == 0) { + return 0; + } + + $left = 0; + $right = $n - 1; + $leftMax = 0; + $rightMax = 0; + $ans = 0; + + while ($left < $right) { + if ($height[$left] < $height[$right]) { + if ($height[$left] > $leftMax) { + $leftMax = $height[$left]; + } else { + $ans += $leftMax - $height[$left]; + } + $left++; + } else { + if ($height[$right] > $rightMax) { + $rightMax = $height[$right]; + } else { + $ans += $rightMax - $height[$right]; + } + $right--; + } + } + return $ans; + } +} +``` + <!-- tabs:end --> <!-- end --> diff --git a/solution/0000-0099/0042.Trapping Rain Water/README_EN.md b/solution/0000-0099/0042.Trapping Rain Water/README_EN.md index b2ef7edc91fed..4c2eb7d876914 100644 --- a/solution/0000-0099/0042.Trapping Rain Water/README_EN.md +++ b/solution/0000-0099/0042.Trapping Rain Water/README_EN.md @@ -181,6 +181,48 @@ public class Solution { } ``` +```php +class Solution { + /** + * @param integer[] $height + * @return integer + */ + + function trap($height) { + $n = count($height); + + if ($n == 0) { + return 0; + } + + $left = 0; + $right = $n - 1; + $leftMax = 0; + $rightMax = 0; + $ans = 0; + + while ($left < $right) { + if ($height[$left] < $height[$right]) { + if ($height[$left] > $leftMax) { + $leftMax = $height[$left]; + } else { + $ans += $leftMax - $height[$left]; + } + $left++; + } else { + if ($height[$right] > $rightMax) { + $rightMax = $height[$right]; + } else { + $ans += $rightMax - $height[$right]; + } + $right--; + } + } + return $ans; + } +} +``` + <!-- tabs:end --> <!-- end --> diff --git a/solution/0000-0099/0042.Trapping Rain Water/Solution.php b/solution/0000-0099/0042.Trapping Rain Water/Solution.php new file mode 100644 index 0000000000000..7458100b61953 --- /dev/null +++ b/solution/0000-0099/0042.Trapping Rain Water/Solution.php @@ -0,0 +1,39 @@ +class Solution { + /** + * @param integer[] $height + * @return integer + */ + + function trap($height) { + $n = count($height); + + if ($n == 0) { + return 0; + } + + $left = 0; + $right = $n - 1; + $leftMax = 0; + $rightMax = 0; + $ans = 0; + + while ($left < $right) { + if ($height[$left] < $height[$right]) { + if ($height[$left] > $leftMax) { + $leftMax = $height[$left]; + } else { + $ans += $leftMax - $height[$left]; + } + $left++; + } else { + if ($height[$right] > $rightMax) { + $rightMax = $height[$right]; + } else { + $ans += $rightMax - $height[$right]; + } + $right--; + } + } + return $ans; + } +} diff --git a/solution/0000-0099/0043.Multiply Strings/README.md b/solution/0000-0099/0043.Multiply Strings/README.md index e89d4911475d3..86eb445be2dc4 100644 --- a/solution/0000-0099/0043.Multiply Strings/README.md +++ b/solution/0000-0099/0043.Multiply Strings/README.md @@ -263,6 +263,38 @@ public class Solution { } ``` +```php +class Solution { + /** + * @param string $num1 + * @param string $num2 + * @return string + */ + + function multiply($num1, $num2) { + $length1 = strlen($num1); + $length2 = strlen($num2); + $product = array_fill(0, $length1 + $length2, 0); + + for ($i = $length1 - 1; $i >= 0; $i--) { + for ($j = $length2 - 1; $j >= 0; $j--) { + $digit1 = intval($num1[$i]); + $digit2 = intval($num2[$j]); + + $temp = $digit1 * $digit2 + $product[$i + $j + 1]; + $product[$i + $j + 1] = $temp % 10; + + $carry = intval($temp / 10); + $product[$i + $j] += $carry; + } + } + $result = implode("", $product); + $result = ltrim($result, '0'); + return $result === "" ? "0" : $result; + } +} +``` + <!-- tabs:end --> <!-- end --> diff --git a/solution/0000-0099/0043.Multiply Strings/README_EN.md b/solution/0000-0099/0043.Multiply Strings/README_EN.md index 53b0d0322c72f..18bd2317bf40d 100644 --- a/solution/0000-0099/0043.Multiply Strings/README_EN.md +++ b/solution/0000-0099/0043.Multiply Strings/README_EN.md @@ -254,6 +254,38 @@ public class Solution { } ``` +```php +class Solution { + /** + * @param string $num1 + * @param string $num2 + * @return string + */ + + function multiply($num1, $num2) { + $length1 = strlen($num1); + $length2 = strlen($num2); + $product = array_fill(0, $length1 + $length2, 0); + + for ($i = $length1 - 1; $i >= 0; $i--) { + for ($j = $length2 - 1; $j >= 0; $j--) { + $digit1 = intval($num1[$i]); + $digit2 = intval($num2[$j]); + + $temp = $digit1 * $digit2 + $product[$i + $j + 1]; + $product[$i + $j + 1] = $temp % 10; + + $carry = intval($temp / 10); + $product[$i + $j] += $carry; + } + } + $result = implode("", $product); + $result = ltrim($result, '0'); + return $result === "" ? "0" : $result; + } +} +``` + <!-- tabs:end --> <!-- end --> diff --git a/solution/0000-0099/0043.Multiply Strings/Solution.php b/solution/0000-0099/0043.Multiply Strings/Solution.php new file mode 100644 index 0000000000000..3ff438105d7e4 --- /dev/null +++ b/solution/0000-0099/0043.Multiply Strings/Solution.php @@ -0,0 +1,29 @@ +class Solution { + /** + * @param string $num1 + * @param string $num2 + * @return string + */ + + function multiply($num1, $num2) { + $length1 = strlen($num1); + $length2 = strlen($num2); + $product = array_fill(0, $length1 + $length2, 0); + + for ($i = $length1 - 1; $i >= 0; $i--) { + for ($j = $length2 - 1; $j >= 0; $j--) { + $digit1 = intval($num1[$i]); + $digit2 = intval($num2[$j]); + + $temp = $digit1 * $digit2 + $product[$i + $j + 1]; + $product[$i + $j + 1] = $temp % 10; + + $carry = intval($temp / 10); + $product[$i + $j] += $carry; + } + } + $result = implode("", $product); + $result = ltrim($result, '0'); + return $result === "" ? "0" : $result; + } +} diff --git a/solution/0000-0099/0044.Wildcard Matching/README.md b/solution/0000-0099/0044.Wildcard Matching/README.md index 801811a4f1fa8..9a5068b5579c3 100644 --- a/solution/0000-0099/0044.Wildcard Matching/README.md +++ b/solution/0000-0099/0044.Wildcard Matching/README.md @@ -400,6 +400,42 @@ function isMatch(s: string, p: string): boolean { } ``` +```php +class Solution { + /** + * @param string $s + * @param string $p + * @return boolean + */ + + function isMatch($s, $p) { + $lengthS = strlen($s); + $lengthP = strlen($p); + $dp = array(); + for ($i = 0; $i <= $lengthS; $i++) { + $dp[$i] = array_fill(0, $lengthP + 1, false); + } + $dp[0][0] = true; + + for ($i = 1; $i <= $lengthP; $i++) { + if ($p[$i - 1] == '*') { + $dp[0][$i] = $dp[0][$i - 1]; + } + } + for ($i = 1; $i <= $lengthS; $i++) { + for ($j = 1; $j <= $lengthP; $j++) { + if ($p[$j - 1] == '?' || $s[$i - 1] == $p[$j - 1]) { + $dp[$i][$j] = $dp[$i - 1][$j - 1]; + } else if ($p[$j - 1] == '*') { + $dp[$i][$j] = $dp[$i][$j - 1] || $dp[$i - 1][$j]; + } + } + } + return $dp[$lengthS][$lengthP]; + } +} +``` + <!-- tabs:end --> <!-- end --> diff --git a/solution/0000-0099/0044.Wildcard Matching/README_EN.md b/solution/0000-0099/0044.Wildcard Matching/README_EN.md index 6f629a309c21f..0affc9936fa44 100644 --- a/solution/0000-0099/0044.Wildcard Matching/README_EN.md +++ b/solution/0000-0099/0044.Wildcard Matching/README_EN.md @@ -393,6 +393,42 @@ function isMatch(s: string, p: string): boolean { } ``` +```php +class Solution { + /** + * @param string $s + * @param string $p + * @return boolean + */ + + function isMatch($s, $p) { + $lengthS = strlen($s); + $lengthP = strlen($p); + $dp = array(); + for ($i = 0; $i <= $lengthS; $i++) { + $dp[$i] = array_fill(0, $lengthP + 1, false); + } + $dp[0][0] = true; + + for ($i = 1; $i <= $lengthP; $i++) { + if ($p[$i - 1] == '*') { + $dp[0][$i] = $dp[0][$i - 1]; + } + } + for ($i = 1; $i <= $lengthS; $i++) { + for ($j = 1; $j <= $lengthP; $j++) { + if ($p[$j - 1] == '?' || $s[$i - 1] == $p[$j - 1]) { + $dp[$i][$j] = $dp[$i - 1][$j - 1]; + } else if ($p[$j - 1] == '*') { + $dp[$i][$j] = $dp[$i][$j - 1] || $dp[$i - 1][$j]; + } + } + } + return $dp[$lengthS][$lengthP]; + } +} +``` + <!-- tabs:end --> <!-- end --> diff --git a/solution/0000-0099/0044.Wildcard Matching/Solution.php b/solution/0000-0099/0044.Wildcard Matching/Solution.php new file mode 100644 index 0000000000000..a6ee3e8bd7922 --- /dev/null +++ b/solution/0000-0099/0044.Wildcard Matching/Solution.php @@ -0,0 +1,33 @@ +class Solution { + /** + * @param string $s + * @param string $p + * @return boolean + */ + + function isMatch($s, $p) { + $lengthS = strlen($s); + $lengthP = strlen($p); + $dp = array(); + for ($i = 0; $i <= $lengthS; $i++) { + $dp[$i] = array_fill(0, $lengthP + 1, false); + } + $dp[0][0] = true; + + for ($i = 1; $i <= $lengthP; $i++) { + if ($p[$i - 1] == '*') { + $dp[0][$i] = $dp[0][$i - 1]; + } + } + for ($i = 1; $i <= $lengthS; $i++) { + for ($j = 1; $j <= $lengthP; $j++) { + if ($p[$j - 1] == '?' || $s[$i - 1] == $p[$j - 1]) { + $dp[$i][$j] = $dp[$i - 1][$j - 1]; + } else if ($p[$j - 1] == '*') { + $dp[$i][$j] = $dp[$i][$j - 1] || $dp[$i - 1][$j]; + } + } + } + return $dp[$lengthS][$lengthP]; + } +} diff --git a/solution/0000-0099/0045.Jump Game II/README.md b/solution/0000-0099/0045.Jump Game II/README.md index 46593c3290fcf..0fd254fd6a654 100644 --- a/solution/0000-0099/0045.Jump Game II/README.md +++ b/solution/0000-0099/0045.Jump Game II/README.md @@ -192,6 +192,30 @@ int jump(int* nums, int numsSize) { } ``` +```php +class Solution { + /** + * @param integer[] $nums + * @return integer + */ + + function jump($nums) { + $maxReach = 0; + $steps = 0; + $lastJump = 0; + for ($i = 0; $i <= count($nums) - 2; $i++) { + $maxReach = max($maxReach, $i + $nums[$i]); + if ($i == $lastJump) { + $lastJump = $maxReach; + $steps++; + } + } + + return $steps; + } +} +``` + <!-- tabs:end --> <!-- end --> diff --git a/solution/0000-0099/0045.Jump Game II/README_EN.md b/solution/0000-0099/0045.Jump Game II/README_EN.md index 261a54e25036a..f4eb53f9d124b 100644 --- a/solution/0000-0099/0045.Jump Game II/README_EN.md +++ b/solution/0000-0099/0045.Jump Game II/README_EN.md @@ -187,6 +187,30 @@ int jump(int* nums, int numsSize) { } ``` +```php +class Solution { + /** + * @param integer[] $nums + * @return integer + */ + + function jump($nums) { + $maxReach = 0; + $steps = 0; + $lastJump = 0; + for ($i = 0; $i <= count($nums) - 2; $i++) { + $maxReach = max($maxReach, $i + $nums[$i]); + if ($i == $lastJump) { + $lastJump = $maxReach; + $steps++; + } + } + + return $steps; + } +} +``` + <!-- tabs:end --> <!-- end --> diff --git a/solution/0000-0099/0045.Jump Game II/Solution.php b/solution/0000-0099/0045.Jump Game II/Solution.php new file mode 100644 index 0000000000000..45c5fb59b475e --- /dev/null +++ b/solution/0000-0099/0045.Jump Game II/Solution.php @@ -0,0 +1,22 @@ +<?php +class Solution { + /** + * @param integer[] $nums + * @return integer + */ + + function jump($nums) { + $maxReach = 0; + $steps = 0; + $lastJump = 0; + for ($i = 0; $i <= count($nums) - 2; $i++) { + $maxReach = max($maxReach, $i + $nums[$i]); + if ($i == $lastJump) { + $lastJump = $maxReach; + $steps++; + } + } + + return $steps; + } +}