当前位置:网站首页>将数组分成和相等的三个部分[问题分析]
将数组分成和相等的三个部分[问题分析]
2022-06-11 23:46:00 【REN_林森】
前言
一题多解,打开视野,见招拆招,融会贯通。
一、将数组分成和相等的三个部分

二、问题多解
1、分3组
// 将数组分成和相等的三个部分。
public class CanThreePartsEqualSum {
/* target:分成三个有序子数组,要求三个子数组和相等。 三个有序子数组和相等,就意味着每个数组和 = sum / 3. 当sum % 3 != 0时,就不可能分成三个相等的有序子数组,毕竟数字不能拆开。 然后遍历数组,寻找连续子数组的和为 sum / 3的,遍历完后,如果能寻找到三个,则成功分组。 */
public boolean canThreePartsEqualSum(int[] arr) {
// 获取数组和。
// 这种调API的方式太慢。
// int total = Arrays.stream(arr).sum();
int total = 0;
for (int i : arr) total += i;
// 判定能否对3取余。
if (total % 3 != 0) return false;
// 得到每组的和。
int target = total / 3;
// 遍历arr,寻找能分成多少组和为target
int n = 0, cur = 0;
for (int i : arr) {
if ((cur += i) == target && (++n) == n) cur = 0;
// 根据bug1/2修改.
if (n >= 3) return true;
}
// 分成三组就成功,否则失败。
// bug1:有可能全部为0.
// return n == 3;
// bug2:有很多对和为0的。
// return n == 3 || n == arr.length;
// return n >= 3;
return false;
}
}
2、分2组
// 按照从左到右遍历完,会有细节问题----存在0归属谁?所以采用n >= 3来判定。
// 如何从两头同时计算,且只计算2组,那么剩下中间一组必为target,可以解决0归属谁的问题--全部归属中间。
class CanThreePartsEqualSum2 {
/* target:分成三个有序子数组,要求三个子数组和相等。 三个有序子数组和相等,就意味着每个数组和 = sum / 3. 当sum % 3 != 0时,就不可能分成三个相等的有序子数组,毕竟数字不能拆开。 然后双指针遍历数组,寻找前后连续子数组的和为 sum / 3的,寻找到两个且中间还有元素,则成功分三组。 */
public boolean canThreePartsEqualSum(int[] arr) {
// 获取数组和。
int total = 0;
for (int i : arr) total += i;
// 判定能否对3取余。
if (total % 3 != 0) return false;
// 得到每组的和。
int target = total / 3;
// 双指针前后遍历数组,寻找左中右三组target
int left = 1, right = arr.length - 2;
int leftSum = arr[0], rightSum = arr[arr.length - 1];
while (left < right) {
if (leftSum != target) leftSum += arr[left++];
if (rightSum != target) rightSum += arr[right--];
if (leftSum == rightSum && rightSum == target && left <= right) return true;
}
return false;
}
}
总结
1)一题多解,打开视野,见招拆招。
参考文献
边栏推荐
- Notes on knowledge points of dynamic planning
- mysql——find_in_set用法
- (linear DP | monotone queue) acwing 895 Longest ascending subsequence
- Wake up wrist - neural network and deep learning (tensorflow application) updating
- 05 classification learning notes lihongyi's in-depth study 2021
- Achievements in science and Technology (XV)
- Here we go! Dragon lizard community enters PKU classroom
- Jenkins basic configuration
- What are the pitfalls of redis's current network: using a cache and paying for disk failures?
- (counting class +dp) acwing 900 Integer partition
猜你喜欢

MySQL 8.0 解压版安装教程

2022 R1 quick opening pressure vessel operation test questions and online simulation test

04 automatic learning rate - learning notes - lihongyi's in-depth learning 2021

Jetpack architecture component learning (3) -- activity results API usage

mysql5和mysql8同时安装

明德扬FPGA开发板XILINX-K7核心板Kintex7 XC7K325 410T工业级

2022 safety officer-a certificate test question simulation test platform operation

(counting class +dp) acwing 900 Integer partition

明德扬ADC系列开发板-Ad9653子板 多通道 高分辨率 高采样率

(simple statistics) acwing 3404 Who are your potential friends
随机推荐
HMS core shows the latest open capabilities in mwc2022, helping developers build high-quality applications
[signals and systems] (XXI) Laplace transform and complex frequency domain analysis -- Laplace transform and its properties
How many steps does it take for C language to become Fibonacci number
Read the logstash principle
mysql——find_in_set用法
Balanced binary tree (AVL tree)
Top selling commodities 【 project mall 】
Integrate工具之Jenkins
Read 5g RF terminal industry
One to one correspondence of multiple schematic diagrams and PCB diagrams under Altium designer project
(linear DP | monotone queue) acwing 895 Longest ascending subsequence
唤醒手腕 - 神经网络与深度学习(Tensorflow应用)更新中
帝国理工等最新《胶囊网络综述》论文,29页pdf阐述胶囊的概念、方法与应用
Altium designer工程下多个原理图和PCB图的一一对应
Setting alias alias and @ reference note
My creation anniversary
A new product with advanced product power, the new third generation Roewe rx5 blind subscription is opened
【BBC learningenglish】with Tango
2022安全员-C证判断题模拟考试平台操作
Jetpack architecture component learning (3) -- activity results API usage