洞察探讨小游戏SDK接入的最佳实践以及对企业跨平台开发的优势
611
2022-11-08
[leetcode] 1013. Partition Array Into Three Parts With Equal Sum
Description
Given an array A of integers, return true if and only if we can partition the array into three non-empty parts with equal sums.
Formally, we can partition the array if we can find indexes i+1 < j with (A[0] + A[1] + … + A[i] == A[i+1] + A[i+2] + … + A[j-1] == A[j] + A[j-1] + … + A[A.length - 1]).
Example 1:
Input: A = [0,2,1,-6,6,-7,9,1,2,0,1]Output: trueExplanation: 0 + 2 + 1 = -6 + 6 - 7 + 9 + 1 = 2 + 0 + 1
Example 2:
Input: A = [0,2,1,-6,6,7,9,-1,2,0,1]Output: false
Example 3:
Input: A = [3,3,6,5,-2,2,5,1,-9,4]Output: trueExplanation: 3 + 3 = 6 = 5 - 2 + 2 + 5 + 1 - 9 + 4
Note:
3 <= A.length <= 50000.-10^4 <= A[i] <= 10^4.
分析
题目的意思是:给你一个数组,判断该数组能否被分为三个相等的子数组。我的思路也很简单,就先求出相等的值是多少,然后再去找该数组可以被划分为多少个这样的相等值的数组,如果小于3个,则返回True,如果大于3个,则要判断能否被3整除,如果不能则判断是否为0.
class Solution: def canThreePartsEqualSum(self, A: List[int]) -> bool: sum_A=sum(A) val=sum_A//3 if(val !=sum_A/3): return False n=len(A) t=0 list_cnt=[] for i in range(n): t+=A[i] if(t==val): list_cnt.append(t) t=0 else: continue if(len(list_cnt)>=3): if(list_cnt[0]==0): return True elif(len(list_cnt)==3): return True return False
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~