Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4dcd9c2

Browse files
authored
feat: add php solution to lc problem: No.2485 (#1142)
1 parent 70fc951 commit 4dcd9c2

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

‎solution/2400-2499/2485.Find the Pivot Integer/README.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,28 @@ function pivotInteger(n: number): number {
201201
}
202202
```
203203

204+
### **PHP**
205+
206+
```php
207+
class Solution {
208+
/**
209+
* @param Integer $n
210+
* @return Integer
211+
*/
212+
function pivotInteger($n) {
213+
$sum = ($n * ($n + 1)) / 2;
214+
$pre = 0;
215+
for ($i = 1; $i <= $n; $i++) {
216+
if ($pre + $i === $sum - $pre) {
217+
return $i;
218+
}
219+
$pre += $i;
220+
}
221+
return -1;
222+
}
223+
}
224+
```
225+
204226
### **...**
205227

206228
```

‎solution/2400-2499/2485.Find the Pivot Integer/README_EN.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,28 @@ function pivotInteger(n: number): number {
164164
}
165165
```
166166

167+
### **PHP**
168+
169+
```php
170+
class Solution {
171+
/**
172+
* @param Integer $n
173+
* @return Integer
174+
*/
175+
function pivotInteger($n) {
176+
$sum = ($n * ($n + 1)) / 2;
177+
$pre = 0;
178+
for ($i = 1; $i <= $n; $i++) {
179+
if ($pre + $i === $sum - $pre) {
180+
return $i;
181+
}
182+
$pre += $i;
183+
}
184+
return -1;
185+
}
186+
}
187+
```
188+
167189
### **...**
168190

169191
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
/**
3+
* @param Integer $n
4+
* @return Integer
5+
*/
6+
function pivotInteger($n) {
7+
$sum = ($n * ($n + 1)) / 2;
8+
$pre = 0;
9+
for ($i = 1; $i <= $n; $i++) {
10+
if ($pre + $i === $sum - $pre) {
11+
return $i;
12+
}
13+
$pre += $i;
14+
}
15+
return -1;
16+
}
17+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /