Skip to content

Commit 9c5b6ae

Browse files
authored
feat: add php solution to lcof2 problem: No.012 (doocs#1109)
1 parent f88efde commit 9c5b6ae

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

lcof2/剑指 Offer II 012. 左右两边子数组的和相等/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,31 @@ public:
143143
};
144144
```
145145

146+
### **PHP**
147+
148+
```php
149+
class Solution {
150+
/**
151+
* @param Integer[] $nums
152+
* @return Integer
153+
*/
154+
function pivotIndex($nums) {
155+
$sum = 0;
156+
$pre = 0;
157+
for ($i = 0; $i < count($nums); $i++) {
158+
$sum += $nums[$i];
159+
}
160+
for ($i = 0; $i < count($nums); $i++) {
161+
if ($pre === $sum - $pre - $nums[$i]) {
162+
return $i;
163+
}
164+
$pre += $nums[$i];
165+
}
166+
return -1;
167+
}
168+
}
169+
```
170+
146171
### **...**
147172

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

0 commit comments

Comments
 (0)