作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/

题目描述

Given an array A of integers, return true if and only if we can partition the array into three non-empty parts with equal sums.

Formally, we can partition the array if we can find indexes i+1 < j with (A[0] + A[1] + ... + A[i] == A[i+1] + A[i+2] + ... + A[j-1] == A[j] + A[j-1] + ... + A[A.length - 1])

Example 1:

Input: [0,2,1,-6,6,-7,9,1,2,0,1]
Output: true
Explanation: 0 + 2 + 1 = -6 + 6 - 7 + 9 + 1 = 2 + 0 + 1

Example 2:

Input: [0,2,1,-6,6,7,9,-1,2,0,1]
Output: false

Example 3:

Input: [3,3,6,5,-2,2,5,1,-9,4]
Output: true
Explanation: 3 + 3 = 6 = 5 - 2 + 2 + 5 + 1 - 9 + 4

Note:

  1. 3 <= A.length <= 50000
  2. -10000 <= A[i] <= 10000

题目大意

给定一个数组能不能分成和相等的连续三部分。每部分至少一个数字。

解题方法

这个题其实很简单,有同学没做出来的原因是把题目的连续条件给忘了、按照回溯法做这个题的话,看到数组A的长度是50000肯定会超时。

既然是连续相等的三部分,那么可以先求出数组总体的和_sum,除以3就得到了每部分的和target。易知这题的时间复杂度要求是O(N),所以当我们进行搜索的时候,只要找到一个连续子子数组的和是target,那么就重新开始计算后面部分的和,统计有多少个连续子数组的和是target。

当我们遍历完成一遍的时候,如果count大于等于3,说明一定可以分成连续相等的三部分。

这里解释下为什么需要count >= 3,因为如果有0出现或者有多个部分的和都是0的话,那么count可以大于3。比如[0,0,0,0,0]的测试用例,count是5,但是也可以分为三个和相等的部分。例如[1, -1, 1, -1, 1, -1, 0],count是4,也可以分为三个和相等的部分。

Python代码如下:

class Solution(object):
def canThreePartsEqualSum(self, A):
"""
:type A: List[int]
:rtype: bool
"""
_sum = sum(A)
if _sum % 3 != 0:
return False
target = _sum / 3
count = 0
cursum = 0
for a in A:
cursum += a
if cursum == target:
count += 1
cursum = 0
return count >= 3

参考资料:https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/discuss/260885/C%2B%2B-6-lines-O(n)-target-sum

日期

2019 年 3 月 24 日 —— 这个周赛太悲催了

【LeetCode】1020. Partition Array Into Three Parts With Equal Sum 解题报告(Python)的更多相关文章

  1. LeetCode 1013 Partition Array Into Three Parts With Equal Sum 解题报告

    题目要求 Given an array A of integers, return true if and only if we can partition the array into three  ...

  2. 【leetcode】1020. Partition Array Into Three Parts With Equal Sum

    题目如下: Given an array A of integers, return true if and only if we can partition the array into three ...

  3. Leetcode 1013. Partition Array Into Three Parts With Equal Sum

    简单题,暴力找出来就行. class Solution: def canThreePartsEqualSum(self, A: List[int]) -> bool: s = sum(A) if ...

  4. [Swift]LeetCode1013. 将数组分成和相等的三个部分 | Partition Array Into Three Parts With Equal Sum

    Given an array A of integers, return true if and only if we can partition the array into three non-e ...

  5. Partition Array Into Three Parts With Equal Sum LT1013

    Given an array A of integers, return true if and only if we can partition the array into three non-e ...

  6. 【LeetCode】548. Split Array with Equal Sum 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 日期 题目地址:https://leetcode ...

  7. 【LeetCode】416. Partition Equal Subset Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 动态规划 日期 题目地址:https://l ...

  8. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  9. 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)

    [LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

随机推荐

  1. 解决CentOS7 docker容器映射端口只监听ipv6的问题

    问题现象 docker容器起来以后,查看9100端口监听情况,如下图: $ ss -lntp State Recv-Q Send-Q Local Address:Port Peer Address:P ...

  2. MapReduce04 框架原理Shuffle

    目录 2 MapReduce工作流程 3 Shuffle机制(重点) 3.1 Shuffle机制 3.2 Partition分区 默认Partitioner分区 自定义Partitioner分区 自定 ...

  3. Scala(一)【安装和IDEA中开发】

    目录 一.下载 二.windows安装 三.linux环境安装 四.Ida开发Scala 1.在线下载Scala插件 2.离线下载Scala插件 3.验证 五.HelloWorld入门程序 1.新建M ...

  4. ssh : connect to host XXX.XXX.XXX.XXX port : 22 connect refused

    初学者 写博客 如有不对之处请多多指教 我是要在俩个主机的俩个虚拟机上 用scp (security copy)进行文件远程复制. 但是 终端 提示 ssh : connect to host XXX ...

  5. SpringBoot让测试类飞起来的方法

    单元测试是项目开发中必不可少的一环,在 SpringBoot 的项目中,我们用 @SpringBootTest 注解来标注一个测试类,在测试类中注入这个接口的实现类之后对每个方法进行单独测试. 比如下 ...

  6. spring注解事务管理

    使用步骤: 步骤一.在spring配置文件中引入<tx:>命名空间<beans xmlns="http://www.springframework.org/schema/b ...

  7. Spring.DM版HelloWorld

    本文主要描述使用Spring.DM2.0,创建OSGi的HelloWorld演示程序,理解Spring.DM的OSGi框架实现机制.   环境描述: 项目 版本 Eclipse 3.7.x JDK 1 ...

  8. JFinal之ActiveRecord开发示例

    JFinal独创Db + Record模式示例 JFinal配备的ActiveRecord插件,除了实现了类似Rails ActiveRecrod的功能之外,还实现了Db + Record模式,此模式 ...

  9. spring-cloud-alibaba-dependencies版本问题

    org.springframework.cloud的spring-cloud-alibaba-dependencies管理的nacos最新版本是0.9.0.RELEASE,已经不再维护了,用起来有版本 ...

  10. 【C#】【MySQL】【GridView】删除出现Parameter index is out of range

    [编程语言]C# [数据库]MySQL [控件]GridView [问题描述]GridView控件中自带[删除],[编辑],[选择],三个按钮[编辑],[选择]正常使用,但是在使用删除时,却报错Par ...