0%

将数组分成和相等的三部分

题目链接:
https://leetcode-cn.com/problems/partition-array-into-three-parts-with-equal-sum/
解法分析:前面部分的思路都很简单,主要说一下最后一行代码为什么要写 >= 而不是 ===,这是因为些特别坑的情况,比如 [0, 0, 0, 0],或者是像 [-1, 1, -1, 1, -1, 1, -1, 1] 这样的情况。可以看出,当和为 0 的时候,可以分成大于三等份的情况必定也可以分成刚好三等份。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* @param {number[]} arr
* @return {boolean}
*/
var canThreePartsEqualSum = function(arr) {
const sum = arr.reduce((pre, cur) => pre + cur);

if(sum % 3 !== 0) {
return false;
}

const part = sum / 3;
let count = 0;
let ans = 0;
for(let i = 0; i < arr.length; i++) {
count += arr[i];
if(count === part) {
ans++;
count = 0;
}
}

return ans >= 3;
};

本文标题:将数组分成和相等的三部分

文章作者:Flower-F

发布时间:2021年12月11日 - 17:57

最后更新:2022年01月19日 - 16:40

-------------本文结束,感谢您的阅读-------------

欢迎关注我的其它发布渠道