leetcode1013将数组分成相等的三部分
作者:互联网
leetcode1013将数组分成相等的三部分
题目详情
题目解答
public class Solution {
public boolean canThreePartsEqualSum(int[] A){
int sum = sum(A, 0, A.length);
if(sum % 3 != 0)
return false;
int aver = sum / 3;
int partSum = 0;
int count = 0;
for(int i = 0; i < A.length; i ++){
partSum += A[i];
if(partSum == aver) {
partSum = 0;
count ++;
}
}
/*
考虑到一下两种特殊情况,因此需要用count来记录
{1,-1,1,-1,1,-1,1,-1}
{1,-1,1,-1}
*/
return partSum == 0 && count >= 3; // 保证分成的份数是大于3 才能被均等分成三份
}
private int sum(int[] A , int start, int end){
int sum = 0;
for(int i = start; i < end; i ++){
sum += A[i];
}
return sum;
}
}
标签:count,leetcode1013,相等,return,int,sum,partSum,++,数组 来源: https://blog.csdn.net/suyongcai1234/article/details/104793857