public boolean splitArraySameAverage(int[] nums) {
if (nums.length == 1) {
return false;
}
int n = nums.length, m = n / 2;
int sum = 0;
for (int num : nums) {
sum += num;
}
for (int i = 0; i < n; i++) {
nums[i] = nums[i] * n - sum;
}
Set<Integer> left = new HashSet<Integer>();
for (int i = 1; i < (1 << m); i++) {
int tot = 0;
for (int j = 0; j < m; j++) {
if ((i & (1 << j)) != 0) {
tot += nums[j];
}
}
if (tot == 0) {
return true;
}
left.add(tot);
}
int rsum = 0;
for (int i = m; i < n; i++) {
rsum += nums[i];
}
for (int i = 1; i < (1 << (n - m)); i++) {
int tot = 0;
for (int j = m; j < n; j++) {
if ((i & (1 << (j - m))) != 0) {
tot += nums[j];
}
}
if (tot == 0 || (rsum != tot && left.contains(-tot))) {
return true;
}
}
return false;
}
08-07
408
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交