题目要求

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])

题目分析及思路

给定一个整数数组,若该数组能分成三个非空数组且各数组的和相等,则返回true。划分数组时不改变原数组的顺序。可以先确定数组的和是否是3的倍数,若是则获取和的1/3值存为s_t。之后遍历数组依次求和。要求先获得s_t值再获取2*s_t值。若能满足要求则返回true。

python代码

class Solution:

def canThreePartsEqualSum(self, A: List[int]) -> bool:

if sum(A) % 3 == 0:

s_t = sum(A) / 3

count, flag1, flag2 = 0, 0, 0

for i in A:

count += i

if count == s_t:

flag1 = 1

if count == 2*s_t and flag1 == 1:

flag2 = 1

if flag1 == 1 and flag2 == 1:

return True

else:

return False

else:

return False

LeetCode 1013 Partition Array Into Three Parts With Equal Sum 解题报告的更多相关文章

  1. 【LeetCode】1020. Partition Array Into Three Parts With Equal Sum 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. Leetcode 1013. Partition Array Into Three Parts With Equal Sum

    简单题,暴力找出来就行. class Solution: def canThreePartsEqualSum(self, A: List[int]) -> bool: s = sum(A) if ...

  3. 【leetcode】1020. Partition Array Into Three Parts With Equal Sum

    题目如下: Given an array A of integers, return true if and only if we can partition the array into three ...

  4. [Swift]LeetCode1013. 将数组分成和相等的三个部分 | Partition Array Into Three Parts With Equal Sum

    Given an array A of integers, return true if and only if we can partition the array into three non-e ...

  5. Partition Array Into Three Parts With Equal Sum LT1013

    Given an array A of integers, return true if and only if we can partition the array into three non-e ...

  6. 【LeetCode】548. Split Array with Equal Sum 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 日期 题目地址:https://leetcode ...

  7. 【LeetCode】416. Partition Equal Subset Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 动态规划 日期 题目地址:https://l ...

  8. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  9. 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)

    [LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

随机推荐

  1. linux安全配置检查脚本_v0.8

    脚本环境:RHEL6.* 脚本说明:该脚本作用为纯执行检测不涉及更改配置等操作,与直接上来就改安全配置等基线脚本相比相对安全一些.虽然如此,在你执行该脚本之前仍然建议你备份或快照一下目标系统. 代码部 ...

  2. 判断回文字符串、回文链表、回文数(python实现)

    所谓回文字符串,就是正读和反读都一样的字符串,比如"level"或者"noon"等等就是回文串.即是对称结构 判断回文字符串 方法一: def is_palin ...

  3. python class和class(object)用法区别

    # -*- coding: utf-8 -*- # 经典类或者旧试类 class A: pass a = A() # 新式类 class B(object): pass b = B() # pytho ...

  4. 11个超震撼的HTML5和纯CSS3动画源码

    1.jQuery多功能手风琴个人信息菜单面板 这是一款基于jQuery的手风琴个人信息菜单面板,每一个菜单项展开后可以自定义布局,因此可以为每一个菜单项实现多功能.类似这样的多功能菜单还有jQuery ...

  5. dma子系统 dmac

    DMA子是CPU中实现数据传输的一种方式,CPU配置好DMA控制器之后发起数据传输,CPU本身不参与数据传输的动作中去. DMA种类: 分为外设DMA和DMA控制器.其中外设DMA实现的为特定的外设与 ...

  6. Specified version of key is not available (44)

    -- ::, ERROR [HiveServer2-Handler-Pool: Thread-]: transport.TSaslTransport (TSaslTransport.java:open ...

  7. halcon之 distance_transform

    Compute the distance transformation of a region   该算子的作用是计算对region转换距离.该算子的形式为distance_transform(Reg ...

  8. 《FPGA全程进阶---实战演练》之搞定阻抗匹配

    笔者最近几天在做视频采集板卡时,视频显示端打算采用 USB2.0接口+上位机 显示,其中USB需要做阻抗匹配.通常情况下USB的阻抗值需要做到90Ω±10%.下面就讲解一下关于阻抗匹配的知识,哪里说得 ...

  9. Glide和Govendor安装和使用

    两个都是Go的包管理工具,二选一 Glide参考:golang 依赖管理 /etc/profile #Go export GOROOT=/home/lintong/software/go export ...

  10. tensorflow finuetuning 例子

    最近研究了下如何使用tensorflow进行finetuning,相比于caffe,tensorflow的finetuning麻烦一些,记录如下: 1.原理 finetuning原理很简单,利用一个在 ...