Description:
Given an array A of integers, return true if and only if we can partition the array into three non-empty parts with equal sums.
Formally, we can partition the array if we can find indexes i+1 < j with (A[0] + A[1] + … + A[i] == A[i+1] + A[i+2] + … + A[j-1] == A[j] + A[j-1] + … + A[A.length - 1])
Example 1:
Example 2:
Example 3:
Note:
- 3 <= A.length <= 50000
- -10000 <= A[i] <= 10000
题意:计算数组A是否能够被,()划分为三个部分,这三个部分不为空且所有元素的和相同;
解法:要将数组A划分为相等的三个部分,并且这三个部分的和相同,那么这三个部分的和应该为数组A所有元素和的三分之一(要求可被3整除);但是,题目已经限定了划分的原则是利用,()分为三个部分,因此难度就大大降低了,因为不需要考虑对任意位置元素的组合;下面是解题的流程
- 计算数组A的和sum
- 计算sum是否能被3整除,若不能,则返回false,否则得到三个部分的和为average = sum / 3
- for i in range(A.length):
- 对元素A[i]求和直到A[i] == average,记录划分数量增加1
- 判断划分数量是否为3,是则返回ture,否则返回false;
Java