Leetcode 1013. Partition Array Into Three Parts With Equal Sum

简单题,暴力找出来就行.

class Solution:
    def canThreePartsEqualSum(self, A: List[int]) -> bool:
        s = sum(A)
        if s % 3 > 0:
            return False
        s /= 3
        size = len(A)
        t = 0
        a, b = -1, -1
        for i in range(size):
            t += A[i]
            if t == s:
                a = i
                break
        t = 0
        for j in range(size - 1, -1, -1):
            t += A[j]
            if t == s:
                b = j
                break
        if a == -1 or b == -1 or a + 1 >= b:
            return False
        return True

猜你喜欢

转载自www.cnblogs.com/zywscq/p/10699001.html