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

856 B
Raw Blame History

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

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

题目

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

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