题目如下:

Given an array A of integers, return true if and only if we can partition the array into three non-emptyparts 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

解题思路:要使得Input能均分成三段,那么Input所有元素的和一定是3的倍数,这里记为3X。假设下标i和j为第一段和第二段的终点,那么有sum(Input[0:i]) = X,sum(Input[0:j]) = 2X。所以只需要用字典记录整个Input从下标0开始到结束每一段的和,判断和是否能被3整除,以及X和2X是否存在并且X出现在2X之前即可。

代码如下:

class Solution(object):
def canThreePartsEqualSum(self, A):
"""
:type A: List[int]
:rtype: bool
"""
dic = {}
total = 0
for i,v in enumerate(A):
total += v
if total not in dic:
dic[total] = [i]
elif len(dic[total]) == 1:
dic[total] += [i]
else:
dic[total][-1] = i
return total % 3 == 0 and total / 3 in dic and total / 3 * 2 in dic and dic[total/3][0] < dic[total / 3 * 2][-1]

【leetcode】1020. Partition Array Into Three Parts With Equal Sum的更多相关文章

  1. 【LeetCode】1020. Partition Array Into Three Parts With Equal Sum 解题报告(Python)

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

  2. 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  ...

  3. [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 ...

  4. 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 ...

  5. 【LeetCode】915. Partition Array into Disjoint Intervals 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/partitio ...

  6. 【leetcode】1043. Partition Array for Maximum Sum

    题目如下: Given an integer array A, you partition the array into (contiguous) subarrays of length at mos ...

  7. 【leetcode】915. Partition Array into Disjoint Intervals

    题目如下: 解题思路:题目要求的是在数组中找到一个下标最小的index,使得index左边(包括自己)子序列的最大值小于或者等于右边序列的最小值.那么我们可以先把数组从最左边开始到数组最右边所有子序列 ...

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

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

  9. 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)

    [LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

随机推荐

  1. 破解Revealapp的试用时间限制

    转载自:http://jingwei6.me/2014/02/28/reveal_crack.html Revealapp作为分析iOS app UI结构的利器,还是非常称手的,89刀的价格也是物有所 ...

  2. (转)详解k8s组件Ingress边缘路由器并落地到微服务 - kubernetes

    转:https://www.cnblogs.com/justmine/p/8991379.html 写在前面 Ingress 英文翻译 进入;进入权;进食,更准确的讲就是入口,即外部流量进入k8s集群 ...

  3. react-native 异常处理 Execution failed for task ':app:mergeDebugResources'.

    1.react-native run-android 失败 * What went wrong:Execution failed for task ':app:mergeDebugResources' ...

  4. ''.startswith() and ''.endswith() instead of string slicing to check for prefixes or suffixes.

    w http://legacy.python.org/dev/peps/pep-0008/ Yes: if foo.startswith('bar'):No:  if foo[:3] == 'bar' ...

  5. C#-概念-基础类库:基础类库

    ylbtech-C#-概念-基础类库:基础类库 基础类库 (BCL) 是微软所提出的一组标准库可提供.NET Framework所有语言使用. 随着 Windows 以及 .NET Framework ...

  6. CycleGAN --- Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks

    文章地址:http://openaccess.thecvf.com/content_ICCV_2017/papers/Zhu_Unpaired_Image-To-Image_Translation_I ...

  7. String 与StringBuffer习题

    1: 画出如下几行代码的结构 // 画出如下几行代码的结构 String s1 = "hello"; // value存储在常量池内 String s2 = "hello ...

  8. win2016

    slmgr /ipk CB7KF-BWN84-R7R2Y-793K2-8XDDG slmgr /skms kms.03k.org slmgr /ato

  9. 关于VMWare的几种网络模式

    具体的可以参考这个博文:http://zhenyaliu.blog.163.com/blog/static/2377571920103775447527/

  10. OSPF多区域配置;骨干区域与非骨干区域;ABR边界路由器;LSA和SPF算法

    SPF:链路状态路由算法.基本用于OSPF中,但是要求路由器路由数据库足够大,因为链路状态信息包括很多内容,这也是一个缺点. OSPF是一种内部网关协议(IGP) OSPF路由协议是一种典型的链路状态 ...