---
title: leetcode-1013
date: 2020-03-29 21:09:22
tags:
categories: leetcode
---

### 1013. 将数组分成和相等的三个部分

[题目](https://leetcode-cn.com/problems/partition-array-into-three-parts-with-equal-sum/)



<!--more-->



因为是整数数组，如果能均分成三份，则数组和肯定是3的倍数。然后遍历数组逐端求和使得和为 sum(A)/3。



```python
class Solution:
  def canThreePartsEqualSum(self, A) -> bool:
    if not A:
      return False
    sa = sum(A)
    if sa % 3:
      return False
    s = sa // 3
    s1 = 0
    s2 = 0


    for i in range(len(A)):
      s1 += A[i]
      if s1 == s and (i+1) < len(A):
        for j in range(len(A[i+1:])):
          s2 += A[i+1+j]
          if s2 == s and j+1 < len(A[i+1:]) and sum(A[i+j+2:])== s:
            return True
    return False
#60 ms	18.7 MB
```

