Skip to content

Commit d6514e4

Browse files
committed
Create 1013.将数组分成和相等的三个部分.js
1 parent 9547cbe commit d6514e4

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} A
3+
* @return {boolean}
4+
*/
5+
var canThreePartsEqualSum = function(A) {
6+
const sum = A.reduce((r, n) => r += n, 0);
7+
if (sum % 3 !== 0) {
8+
return false;
9+
}
10+
11+
const avg = sum / 3;
12+
let cur = 0;
13+
let count = 0;
14+
for (const a of A) {
15+
cur += a;
16+
if (cur === avg) {
17+
cur = 0;
18+
count++;
19+
}
20+
}
21+
return count >= 3;
22+
};

0 commit comments

Comments
 (0)