From a933ba539407df2628c21d863e4c8da827bba7a1 Mon Sep 17 00:00:00 2001 From: Qiu-IT Date: Wed, 5 Jul 2023 08:41:13 +0200 Subject: [PATCH] feat: add php solution to lc problem: No.2485 --- .../2485.Find the Pivot Integer/README.md | 22 +++++++++++++++++++ .../2485.Find the Pivot Integer/README_EN.md | 22 +++++++++++++++++++ .../2485.Find the Pivot Integer/Solution.php | 17 ++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 solution/2400-2499/2485.Find the Pivot Integer/Solution.php diff --git a/solution/2400-2499/2485.Find the Pivot Integer/README.md b/solution/2400-2499/2485.Find the Pivot Integer/README.md index a48c3d8c8f7da..c9b4db337dceb 100644 --- a/solution/2400-2499/2485.Find the Pivot Integer/README.md +++ b/solution/2400-2499/2485.Find the Pivot Integer/README.md @@ -201,6 +201,28 @@ function pivotInteger(n: number): number { } ``` +### **PHP** + +```php +class Solution { + /** + * @param Integer $n + * @return Integer + */ + function pivotInteger($n) { + $sum = ($n * ($n + 1)) / 2; + $pre = 0; + for ($i = 1; $i <= $n; $i++) { + if ($pre + $i === $sum - $pre) { + return $i; + } + $pre += $i; + } + return -1; + } +} +``` + ### **...** ``` diff --git a/solution/2400-2499/2485.Find the Pivot Integer/README_EN.md b/solution/2400-2499/2485.Find the Pivot Integer/README_EN.md index e4660c1925ec0..8407038773666 100644 --- a/solution/2400-2499/2485.Find the Pivot Integer/README_EN.md +++ b/solution/2400-2499/2485.Find the Pivot Integer/README_EN.md @@ -164,6 +164,28 @@ function pivotInteger(n: number): number { } ``` +### **PHP** + +```php +class Solution { + /** + * @param Integer $n + * @return Integer + */ + function pivotInteger($n) { + $sum = ($n * ($n + 1)) / 2; + $pre = 0; + for ($i = 1; $i <= $n; $i++) { + if ($pre + $i === $sum - $pre) { + return $i; + } + $pre += $i; + } + return -1; + } +} +``` + ### **...** ``` diff --git a/solution/2400-2499/2485.Find the Pivot Integer/Solution.php b/solution/2400-2499/2485.Find the Pivot Integer/Solution.php new file mode 100644 index 0000000000000..3ff4d71c1ba5a --- /dev/null +++ b/solution/2400-2499/2485.Find the Pivot Integer/Solution.php @@ -0,0 +1,17 @@ +class Solution { + /** + * @param Integer $n + * @return Integer + */ + function pivotInteger($n) { + $sum = ($n * ($n + 1)) / 2; + $pre = 0; + for ($i = 1; $i <= $n; $i++) { + if ($pre + $i === $sum - $pre) { + return $i; + } + $pre += $i; + } + return -1; + } +}