作者: 负雪明烛
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. error while loading shared libraries: libstdc++.so.5: wrong ELF class: ELFCLASS64

    今天部署一个探针在运行的时候报了这样一个错:error while loading shared libraries: libstdc++.so.5: wrong ELF class: ELFCLAS ...

  2. KEGG通路图应该怎么看(转载)

    转载:http://www.omicshare.com/forum/thread-107-1-3219.html (出处: OmicShare Forum) 不管是RNA-seq的分析数据,还是蛋白组 ...

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

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

  4. 学习java的第五天

    一.今日收获 1.java程序设计完全手册第一章节的小总结 2.完成了部分例题验证解答. 二.今日难题 1.java语言与c++语言的不同点. 2.有例题验证不出来 三.明日目标 1.复习java程序 ...

  5. Oracle—表、约束、索引、表空间、分区、序列、统计信息

    表.约束.索引.表空间.分区.序列.统计信息 一.表及其操作 1.创建表 create table 表名 ( 字段名1 字段类型 默认值 是否为空 , 字段名2 字段类型 默认值 是否为空, 字段名3 ...

  6. MySQL学习(一)——创建新用户、数据库、授权

    一.创建用户 1.登录mysql mysql -u root -p 2.创建本地用户>/font> use mysql; //选择mysql数据库 create user 'test'@' ...

  7. JQuery 和 CSS 等选择器:

    JQuery 选择器: CSS 选择器:

  8. FastDFS的理解和分析

    FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储.文件同步.文件访问(文件上传.文件下载)等,解决了大容量存储和负载均衡的问题.特别适合以文件为载体的在线服务,如相 ...

  9. Git初始化及仓库创建和操作

    一.基本信息配置 1.全局配置用户名 git config --global user.name "YeHuan-byte" 2.全局配置邮箱 git config --globa ...

  10. Quartz使用AutoFac依赖注入问题小结

    theme: channing-cyan highlight: a11y-dark 背景 最近在做一个需求,就是在Job中捕捉异常,然后通过邮件或者消息的方式推送给指定人员,在需求实现的过程中遇到的一 ...