blog/source/_posts/2020-03-29-leetcode-1013.md
Ching 9690121403 feat(init project): add all existing files
add all existing files

Signed-off-by: Ching <loooching@gmail.com>
2022-02-02 19:04:18 +08:00

46 lines
856 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
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
```