问题描述:canBalance问题
Given a non-empty array, return true if there is a place to split the array so that the sum of the numbers on one side is equal to the sum of the numbers on the other side.
canBalance([1, 1, 1, 2, 1]) → true
canBalance([2, 1, 1, 2, 1]) → false
canBalance([10, 10]) → true
代码:
public boolean canBalance(int[] nums) {
int sum=0,left=0;
for(int i:nums)
sum += i;
if(sum%2!=0) return false;
int half = sum / 2;
for(int j:nums) {
left += j;
if(left==half) return true;
else if(left>half) return false;
}
return false;
}