【leetcode】1020. Partition Array Into Three Parts With Equal Sum
题目如下:
Given an array
Aof integers, returntrueif and only if we can partition the array into three non-emptyparts with equal sums.Formally, we can partition the array if we can find indexes
i+1 < jwith(A[0] + A[1] + ... + A[i] == A[i+1] + A[i+2] + ... + A[j-1] == A[j] + A[j-1] + ... + A[A.length - 1])Example 1:
Input: [0,2,1,-6,6,-7,9,1,2,0,1]
Output: true
Explanation: 0 + 2 + 1 = -6 + 6 - 7 + 9 + 1 = 2 + 0 + 1Example 2:
Input: [0,2,1,-6,6,7,9,-1,2,0,1]
Output: falseExample 3:
Input: [3,3,6,5,-2,2,5,1,-9,4]
Output: true
Explanation: 3 + 3 = 6 = 5 - 2 + 2 + 5 + 1 - 9 + 4Note:
3 <= A.length <= 50000-10000 <= A[i] <= 10000
解题思路:要使得Input能均分成三段,那么Input所有元素的和一定是3的倍数,这里记为3X。假设下标i和j为第一段和第二段的终点,那么有sum(Input[0:i]) = X,sum(Input[0:j]) = 2X。所以只需要用字典记录整个Input从下标0开始到结束每一段的和,判断和是否能被3整除,以及X和2X是否存在并且X出现在2X之前即可。
代码如下:
class Solution(object):
def canThreePartsEqualSum(self, A):
"""
:type A: List[int]
:rtype: bool
"""
dic = {}
total = 0
for i,v in enumerate(A):
total += v
if total not in dic:
dic[total] = [i]
elif len(dic[total]) == 1:
dic[total] += [i]
else:
dic[total][-1] = i
return total % 3 == 0 and total / 3 in dic and total / 3 * 2 in dic and dic[total/3][0] < dic[total / 3 * 2][-1]
【leetcode】1020. 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 解题报告
题目要求 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】915. Partition Array into Disjoint Intervals 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/partitio ...
- 【leetcode】1043. Partition Array for Maximum Sum
题目如下: Given an integer array A, you partition the array into (contiguous) subarrays of length at mos ...
- 【leetcode】915. Partition Array into Disjoint Intervals
题目如下: 解题思路:题目要求的是在数组中找到一个下标最小的index,使得index左边(包括自己)子序列的最大值小于或者等于右边序列的最小值.那么我们可以先把数组从最左边开始到数组最右边所有子序列 ...
- Leetcode 1013. Partition Array Into Three Parts With Equal Sum
简单题,暴力找出来就行. class Solution: def canThreePartsEqualSum(self, A: List[int]) -> bool: s = sum(A) if ...
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
随机推荐
- XML大作业
XML大作业 共两题,均于实验上机完成 第一题:在xml文档中使用DTD 第二题:掌握使用xsl显示xml文件的基本方法 第一题: 一.实验目的 (1)通过本实验,使学生能够了解并掌握XML DTD的 ...
- [CSP-S模拟测试]:甜圈(线段树)
题目描述 $D$先生,是一个了不起的甜甜圈制造商.今天,他的厨房准备在日出之前制作甜甜圈.$D$先生瞬间完成了$N$个油炸圈饼.但是,这些油炸圈饼得先经过各种装饰任务才可以成为甜甜圈销售:填充奶油,浸 ...
- (转)使用windows server2008 创建 Hyper-V虚拟机
转:https://jingyan.baidu.com/article/7c6fb42833ad4980652c904f.html Hyper-v是微软提供的虚拟机,利用server 2008搭建hy ...
- BF语言学习
Brainfuck是一种极小化的计算机语言,它是由Urban Müller在1993年创建的.由于fuck在英语中是脏话,这种语言有时被称为brainf*ck或brainf**k,甚至被简称为BF.这 ...
- Malformed UTF-8 characters, possibly incorrectly encoded 或中文乱码 (Uncaught InvalidArgumentException: Malformed UTF-8 characters, possibly incorrectly encoded in)
问题: Uncaught InvalidArgumentException: Malformed UTF-8 characters, possibly incorrectly encoded in 是 ...
- UltraISO 9.6.1.3016(带注册机)
UltraISO 9.6.1.3016 链接: http://pan.baidu.com/s/1kTqO6hD密码: ehdc
- StringTokenizer拆分字符串
今天要做一个过滤特殊字符的需求, 看了下公司以前过滤特俗字符代码, 用的居然是 StringTokenizer, 完全不熟悉啊, 于是恶补了一下, 把StringTokenizer在JDK中的文档也翻 ...
- Unity3D移动端电量与wifi信号的获取
移动端游戏中无法看到电量与wifi信号对于玩家来说是很困扰的事. 关于这个问题安卓与iOS有不同的方法 电量 安卓 安卓获取电量有两种方法,一种是读取安卓手机里的一个文件,一种是利用安卓与Unity互 ...
- HDU 1269 迷宫城堡 (Kosaraju)
题目链接:HDU 1269 Problem Description 为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000), ...
- iframe父页面和子页面高度自适应
父页HTML: <iframe id="mainframe" name="mainframe" style="width:100%;&quo ...