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 ...
随机推荐
- MySQL利用xtrabackup在线修复或新增从库
如果数据库的数据量很大,表大小有几十个G,利用mysqldump导出备份会消耗非常长的时间,会对数据库产生不稳定风险,这时可以利用xtrabackup工具在线复制主库文件,利用复制出来的主库文件可以修 ...
- 关于asyncio知识(二)
一.asyncio之—-入门初探 通过上一篇关于asyncio的整体介绍,看过之后基本对asyncio就有一个基本认识,如果是感兴趣的小伙伴相信也会尝试写一些小代码尝试用了,那么这篇文章会通过一个简单 ...
- RxSwift之路 1#Swift语法知识准备
RxSwift之路 1#Swift语法知识准备 在开始学习 RxSwift 之前,一定要对 Swift 相关语法有所了解,否则就很难理解为什么可以这样.关于 Swift 的学习其实只要看看 Swift ...
- js判断移动端是否安装某软软件,安装直接打开相应的链接,否则跳转到下载商店方法
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- Hadoop源码系列(一)FairScheduler申请和分配container的过程
1.如何申请资源 1.1 如何启动AM并申请资源 1.1.1 如何启动AM val yarnClient = YarnClient.createYarnClient setupCredentials( ...
- MySQL yum 在线与本地包方式安装
以下实践操作是在 liux-centos7 上安装配置 先检测是否安装mysql,然后在拆卸mysql # 检测[root@localhost ~]# yum list installed | gr ...
- Python学习小组微信群公告页面
<简明 Python 教程>读经群,PDF地址:https://pan.baidu.com/s/1FK8s4cTfwWxSktOfS95ArQ,PyCharm-Edu地址:https:// ...
- VS的ASP.NET项目中cshtml关键词出错 红线,当前上下文中不存在名称
[参考]VS的ASP.NET项目中cshtml突然出错,当前上下文中不存在名称“ViewBag” 原因:web.config 配置错误 这种情况是因为两个web.config文件版本不匹配,需要进行修 ...
- Ubuntu下Ansible安装和使用
Ansible是一个批量部署的工具 参考:Ansible中文权威指南 1.安装 sudo apt-get install software-properties-common sudo apt-add ...
- python if,for,while
# -*- coding:utf-8 -*- # 第四章 if for while #布尔逻辑 print True == False print True and False print True ...