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 ...
随机推荐
- IoC之AutoFac(二)——解析服务
阅读目录 一 Resolve方法 二 TryResolve和ResolveOptional方法 三 解析服务时传参 3.1 可用参数类型 3.2 带反射组件的参数 3.3 具有Lambda表达式组件的 ...
- IDEA使用笔记(九)——设置文件注释
方式一:后写文件描述信息 1:设置——如下图所示 2:验证——创建个类试试 3:验证——结果如下 4:其他,所有注释模版中包含 #parse("File Header.java") ...
- PEP 492 -- Coroutines with async and await syntax 翻译
因为工作中慢慢开始用python的协程,所以想更好的理解一下实现方式,故翻译此文 原文中把词汇表放到最后,但是我个人觉得放在最开始比较好,这样可以增加当你看原文时的理解程度 词汇表 原生协程函数 Na ...
- Irrlicht 3D Engine 笔记系列 之 自己定义Animator
作者: i_dovelemon 日期: 2014 / 12 / 17 来源: CSDN 主题: Custom Animator, Referenced count 引言 在昨天的文章<Irrli ...
- 让Elasticsearch集群冷热分离、读写分离【转】
转自:https://blog.csdn.net/jiao_fuyou/article/details/50511255 根据Elasticsearch中文社区<ES冷热分离(读写分离) hot ...
- 【Git】git pull和git pull --rebase的使用
git pull = git fetch + git mergegit pull --rebase = git fetch + git rebase 现在来看看git merge和git rebase ...
- Maven知识总结(转)
原文地址:http://blog.csdn.net/caihaijiang/article/details/6664910 1.Maven内置变量说明: ${basedir} 项目根目录 ${proj ...
- PHP正则验证类
项目中经常使用到的验证,很使用的.查看与下载<?php /** * 验证类 * * @lastmodify 2015-12-19 * @author wuheng */ class Verify ...
- Vue.js之Vue计算属性、侦听器、样式绑定
前言 上一篇介绍了Vue的基本概念,这一篇介绍一下Vue的基本使用. 一.搭建一个Vue程序 1.1 搭建Vue环境 搭建Vue的开发环境总共有三种方法: 引入CDN <script src=& ...
- python让实例作用于for循环并当做list来使用
python如果想让一个类被用于for....in 循环,类型list和tuple那样,可以实现__iter__方法. 这个方法返回一个迭代对象,python的for循环就会不断调用该迭代对象的ne ...