题目要求

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. 【PHP】解析PHP的GD库

    官方文档:http://php.net/manual/en/book.image.php 1.GD库简介 PHP可以创建和操作多种不同格式的图像文件.PHP提供了一些内置的图像信息函数,也可以使用GD ...

  2. Mathematica 11.1.0 下载及注册流程

    新版本注册机: http://files.cnblogs.com/files/dabaopku/Mathematica_11.1.0_Keygen.exe.zip 类似于11.0, 在控制台运行, 比 ...

  3. TensorFlow 图片resize方法

    参见这篇博客 tensorflow里面用于改变图像大小的函数是tf.image.resize_images(image, (w, h), method):image表示需要改变此存的图像,第二个参数改 ...

  4. hdoj:2075

    A|B? Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  5. linux清理日志脚本

    1.删除日志的命令 find 目录路径 -mtime +天数 -name "文件名" -exec rm -rf {} \; 例如:#!/bin/bash find /usr/loc ...

  6. mybatis打印完整的sql

    mybatis log plugin

  7. 05Hadoop-左外连接

    场景:有两张表,一张用户表(user),交易表(transactions).两张表的字段如下: 两份表数据做个左连接,查询出(商品名,地址)这种格式. 这样就是相当于交易表是左表,不管怎么样数据都要保 ...

  8. 通用Mapper新特性:ExampleBuilder 2017年12月18日

    package tk.mybatis.mapper.test.example; import org.apache.ibatis.session.SqlSession; import org.juni ...

  9. 内建模块 datetime使用

    #-*- coding:utf-8 -*- from datetime import datetime #获取当前日期和时间 now =datetime.now() print now # 2017- ...

  10. box-shadow比较美观的阴影

    无奈每次调阴影都特别丑,这次我把它记下来,下次不调了 box-shadow: 0 5px 15px -5px rgba(0,0,0,.5);