LeetCode 1013 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-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 解题报告的更多相关文章
- 【LeetCode】1020. Partition Array Into Three Parts With Equal Sum 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- Leetcode 1013. Partition Array Into Three Parts With Equal Sum
简单题,暴力找出来就行. class Solution: def canThreePartsEqualSum(self, A: List[int]) -> bool: s = sum(A) if ...
- 【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 ...
- [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 ...
- 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 ...
- 【LeetCode】548. Split Array with Equal Sum 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 日期 题目地址:https://leetcode ...
- 【LeetCode】416. Partition Equal Subset Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 动态规划 日期 题目地址:https://l ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)
[LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
随机推荐
- 通过__block的作用深入研究block
block普通引用 默认情况下,在block中访问外部变量是通过复制一个变量来操作的,既可以读,但是写操作不对原变量生效,下面通过代码来举证 NSString *a = @"testa&qu ...
- OpenLayers WorkShop 快速学习通道
学习地址:https://openlayers.org/workshop/en/ OpenLayers Workshop Introduction Basics Creating a map Zoom ...
- mysql update ...select的使用 & update 遇到 disable safe 的解决方法
use `testdb`; update dtable d INNER JOIN new_table n ON d.details = n.details set d.email = n.email, ...
- 从MySQL Bug#67718浅谈B+树索引的分裂优化(转)
原文链接:http://hedengcheng.com/?p=525 问题背景 今天,看到Twitter的DBA团队发布了其最新的MySQL分支:Changes in Twitter MySQL 5. ...
- 解决Chrome 70版本以后谷歌不再信任赛门铁克证书问题
Google 从 2018 年 10 月发布的 Chrome 70 就停止信任赛门铁克的旧证书了,而 Mozilla 也将在 10 月底发布 Firefox 63 时停止信任赛门铁克的旧证书. 导致大 ...
- SQL 性能分析
SELECT s2.dbid, s1.sql_handle, (SELECT TOP SUBSTRING(s2.text,statement_start_offset / + , ( (CASE WH ...
- PCL_common模块api代码解析
pcl_common库包含大多数PCL库使用的公共数据结构和方法.核心数据结构包括PointCloud类和许多用于表示点.表面法线.RGB颜色值.特征描述符等的点类型.它还包含许多用于计算距离/范数. ...
- Win10系统安装UEFI+GPT配置
借鉴博文 https://blog.csdn.net/lyj_viviani/article/details/51800445 安装win10需要改用UEFI引导模式,硬盘需要换成GUID格式; ...
- [原创]Cadence Allegro小技巧之解决Out of date shapes问题
Allegro报错“Dynamic shapes are out of date; please update them. Check for out of date shapes in Setup ...
- centos7.3安装nvidia驱动和cuda9
一, 挂载本地源镜像 1) 下载操作系统镜像 所有服务器操作系统必须统一,本平台只支持 CentOS 7.3 1611,镜像下载地址. 2) 上传镜像到服务器 ,假设上传在 root 下 3) 建立挂 ...