思路
先求总和,再遍历求和,每当达到总和的1/3就重置
code
func canThreePartsEqualSum(A []int) bool {
if len(A) < 3 {
return false
}
total := 0
for _, v := range A {
total += v
}
if total%3 != 0 {
return false
}
sum := 0
for _, v := range A {
sum += v
if sum == total/3 {
sum = 0
}
}
return sum == 0
}
更多内容请移步我的repo:https://github.com/anakin/golang-leetcode