From a3f7adf17d92847105ddf27c96ac59b7cfb25a56 Mon Sep 17 00:00:00 2001 From: Qiu-IT Date: Sun, 16 Apr 2023 20:00:30 +0200 Subject: [PATCH] feat: add php solution to lc problem: No.0977 --- .../0977.Squares of a Sorted Array/README.md | 29 +++++++++++++++++++ .../README_EN.md | 29 +++++++++++++++++++ .../Solution.php | 24 +++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 solution/0900-0999/0977.Squares of a Sorted Array/Solution.php diff --git a/solution/0900-0999/0977.Squares of a Sorted Array/README.md b/solution/0900-0999/0977.Squares of a Sorted Array/README.md index f2e342b318a6f..104e95f624293 100644 --- a/solution/0900-0999/0977.Squares of a Sorted Array/README.md +++ b/solution/0900-0999/0977.Squares of a Sorted Array/README.md @@ -221,6 +221,35 @@ impl Solution { } ``` +### **PHP** + +```php +class Solution { + /** + * @param Integer[] $nums + * @return Integer[] + */ + function sortedSquares($nums) { + $i = 0; + $j = $k = count($nums) - 1; + $rs = array_fill(0, count($nums), -1); + while ($i <= $j) { + $max1 = $nums[$i] * $nums[$i]; + $max2 = $nums[$j] * $nums[$j]; + if ($max1 > $max2) { + $rs[$k] = $max1; + $i++; + } else { + $rs[$k] = $max2; + $j--; + } + $k--; + } + return $rs; + } +} +``` + ### **...** ``` diff --git a/solution/0900-0999/0977.Squares of a Sorted Array/README_EN.md b/solution/0900-0999/0977.Squares of a Sorted Array/README_EN.md index 3449e157e5e6e..6da87134087dc 100644 --- a/solution/0900-0999/0977.Squares of a Sorted Array/README_EN.md +++ b/solution/0900-0999/0977.Squares of a Sorted Array/README_EN.md @@ -169,6 +169,35 @@ impl Solution { } ``` +### **PHP** + +```php +class Solution { + /** + * @param Integer[] $nums + * @return Integer[] + */ + function sortedSquares($nums) { + $i = 0; + $j = $k = count($nums) - 1; + $rs = array_fill(0, count($nums), -1); + while ($i <= $j) { + $max1 = $nums[$i] * $nums[$i]; + $max2 = $nums[$j] * $nums[$j]; + if ($max1 > $max2) { + $rs[$k] = $max1; + $i++; + } else { + $rs[$k] = $max2; + $j--; + } + $k--; + } + return $rs; + } +} +``` + ### **...** ``` diff --git a/solution/0900-0999/0977.Squares of a Sorted Array/Solution.php b/solution/0900-0999/0977.Squares of a Sorted Array/Solution.php new file mode 100644 index 0000000000000..7bda360d327a5 --- /dev/null +++ b/solution/0900-0999/0977.Squares of a Sorted Array/Solution.php @@ -0,0 +1,24 @@ +class Solution { + /** + * @param Integer[] $nums + * @return Integer[] + */ + function sortedSquares($nums) { + $i = 0; + $j = $k = count($nums) - 1; + $rs = array_fill(0, count($nums), -1); + while ($i <= $j) { + $max1 = $nums[$i] * $nums[$i]; + $max2 = $nums[$j] * $nums[$j]; + if ($max1 > $max2) { + $rs[$k] = $max1; + $i++; + } else { + $rs[$k] = $max2; + $j--; + } + $k--; + } + return $rs; + } +} \ No newline at end of file