-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray1013.java
More file actions
64 lines (63 loc) · 1.85 KB
/
Array1013.java
File metadata and controls
64 lines (63 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package array;
/**
* @ProjectName: leetcode
* @Package: array
* @ClassName: Array1013
* @Author: markey
* @Description:1013. 将数组分成和相等的三个部分
* 给你一个整数数组 A,只有可以将其划分为三个和相等的非空部分时才返回 true,否则返回 false。
*
* 形式上,如果可以找出索引 i+1 < j 且满足 (A[0] + A[1] + ... + A[i] == A[i+1] + A[i+2] + ... + A[j-1] == A[j] + A[j-1] + ... + A[A.length - 1]) 就可以将数组三等分。
*
* 示例 1:
*
* 输出:[0,2,1,-6,6,-7,9,1,2,0,1]
* 输出:true
* 解释:0 + 2 + 1 = -6 + 6 - 7 + 9 + 1 = 2 + 0 + 1
* 示例 2:
*
* 输入:[0,2,1,-6,6,7,9,-1,2,0,1]
* 输出:false
* 示例 3:
*
* 输入:[3,3,6,5,-2,2,5,1,-9,4]
* 输出:true
* 解释:3 + 3 = 6 = 5 - 2 + 2 + 5 + 1 - 9 + 4
*
* 提示:
*
* 3 <= A.length <= 50000
* -10^4 <= A[i] <= 10^4
*
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/partition-array-into-three-parts-with-equal-sum
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
* @Date: 2020/3/11 22:17
* @Version: 1.0
*/
public class Array1013 {
public boolean canThreePartsEqualSum(int[] A) {
int sum = 0;
for (int i = 0; i < A.length; i++) {
sum += A[i];
}
if (sum % 3 != 0) {
return false;
}
sum = sum /3;
int index1, index2, sum1 = 0, sum2 = 0;
for (index1 = 0; index1 < A.length; index1++) {
sum1 += A[index1];
if (sum1 == sum) {
break;
}
}
for (index2 = index1 + 1; index2 < A.length; index2++) {
sum2 += A[index2];
if (sum2 == sum && index2 < A.length - 1) {
return true;
}
}
return false;
}
}