【Leetcode_总结】1013. 将数组分成和相等的三个部分 - python

Q:

给定一个整数数组 A,只有我们可以将其划分为三个和相等的非空部分时才返回 true,否则返回 false

形式上,如果我们可以找出索引 i+1 < j 且满足 (A[0] + A[1] + ... + A[i] == A[i+1] + A[i+2] + ... + A[j-1] == A[j] + A[j-1] + ... + A[A.length - 1]) 就可以将数组三等分。

示例 1:

输出:[0,2,1,-6,6,-7,9,1,2,0,1]
输出:true
解释:0 + 2 + 1 = -6 + 6 - 7 + 9 + 1 = 2 + 0 + 1

链接:https://leetcode-cn.com/problems/partition-array-into-three-parts-with-equal-sum/

思路:

代码:

class Solution:
    def canThreePartsEqualSum(self, A: List[int]) -> bool:
        t,s,times = 0,sum(A)//3,3
        for i in A:
            t += i
            if t == s:
                t = 0
                times -= 1
        return times == 0

猜你喜欢

转载自blog.csdn.net/maka_uir/article/details/88969961
今日推荐