题目如下:

Given an array A of 0s and 1s, divide the array into 3 non-empty parts such that all of these parts represent the same binary value.

If it is possible, return any [i, j] with i+1 < j, such that:

  • A[0], A[1], ..., A[i] is the first part;
  • A[i+1], A[i+2], ..., A[j-1] is the second part, and
  • A[j], A[j+1], ..., A[A.length - 1] is the third part.
  • All three parts have equal binary value.

If it is not possible, return [-1, -1].

Note that the entire part is used when considering what binary value it represents.  For example, [1,1,0] represents 6 in decimal, not 3.  Also, leading zeros are allowed, so [0,1,1]and [1,1] represent the same value.

Example 1:

Input: [1,0,1,0,1]
Output: [0,3]

Example 2:

Input: [1,1,0,1,1]
Output: [-1,-1]

Note:

  1. 3 <= A.length <= 30000
  2. A[i] == 0 or A[i] == 1

解题思路:可以肯定的一点是A中1的个数(记为count_1)一定是3的倍数,如果不是那么是无法分成相同的三等份的;如果可以等分,那么每一份的1的个数count_1/3,假设每一份的有效字符串是S(有效字符串可以理解成不包括前面的0),A就可以表示成 0...0S0...0S0...0S 的形式。接下来倒序遍历A,遍历过程中记录1的数量,每当数量到达count_1/3的时候将这一段记为其中一等份,那么A就可以分成 [S0...0,S0...0,S]的形式,记为parts数组,第一个S前的0是无效的,所以不需要处理。接下来就只要判断parts数组中最后一个元素是否是前两个元素的前缀,如果是的话,就表示可以被等分了。最后是求等分的下标位置,只要把[S0...0,S0...0,S] 中S后面的0都给下一个元素变成[S,0...0S,0...0S]就可以轻而易举的得到结果了。

代码如下:

class Solution(object):
def threeEqualParts(self, A):
"""
:type A: List[int]
:rtype: List[int]
"""
A = [str(i) for i in A]
count_1 = A.count('')
if count_1 == 0 :
return [0,2]
elif count_1 == 0 or count_1 % 3 != 0:
return [-1,-1] parts = ['','','']
times = 0
inx = 2
subs = ''
for i,v in enumerate(A[::-1]):
if v == '':
times += 1
subs = v + subs
if times == (count_1 / 3):
parts[inx] = subs
inx -= 1
times = 0
subs = ''
#print parts
if parts[0].startswith(parts[2]) and parts[1].startswith(parts[2]):
second_len = len(parts[2]) + (len(parts[1]) - len(parts[2]))
first_len = len(parts[2]) + len(parts[1]) + + (len(parts[0]) - len(parts[2]))
return [len(A) - first_len-1,len(A) - second_len]
#pass
#print parts
return [-1,-1]

【leetcode】927. Three Equal Parts的更多相关文章

  1. 【LeetCode】416. Partition Equal Subset Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 动态规划 日期 题目地址:https://l ...

  2. 【leetcode】1208. Get Equal Substrings Within Budget

    题目如下: You are given two strings s and t of the same length. You want to change s to t. Changing the  ...

  3. 【leetcode】1224. Maximum Equal Frequency

    题目如下: Given an array nums of positive integers, return the longest possible length of an array prefi ...

  4. 【leetcode】416. Partition Equal Subset Sum

    题目如下: 解题思路:对于这种判断是否的题目,首先看看动态规划能不能解决.本题可以看成是从nums中任选i个元素,判断其和是否为sum(nums)/2,很显然从nums中任选i个元素的和的取值范围是[ ...

  5. 【leetcode】698. Partition to K Equal Sum Subsets

    题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...

  6. 【leetcode】486. Predict the Winner

    题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...

  7. 【leetcode】668. Kth Smallest Number in Multiplication Table

    题目如下: 解题思路:几乎和[leetcode]719. Find K-th Smallest Pair Distance 的方法一样.只不过一个是减法一个是乘法,还有一点区别是[leetcode]7 ...

  8. 【LeetCode】760. Find Anagram Mappings 解题报告

    [LeetCode]760. Find Anagram Mappings 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/find ...

  9. 【LeetCode】299. Bulls and Cows 解题报告(Python)

    [LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

随机推荐

  1. pytest 教程

    https://blog.csdn.net/lb245557472/article/details/90341297 https://blog.csdn.net/yyang3121/article/d ...

  2. archetypeCatalog=internal

    Properties中添加  archetypeCatalog=internal,不加这个参数,在maven生成骨架的时候将会非常慢.

  3. linux mail 发送邮件

    1.mail -s "Title“ 123456@163.com   只有主题,没有内容 2. 有邮件内容 echo "text" | mail -s "Tit ...

  4. Dataphin的代码自动化能力如何助力商业决策

    前言 随着大数据趋势的迅速增长,数据的重要性与日俱增,企业内看数据.用数据的诉求越来越强烈,其中最常见的就是各种经营报表数据:老板每日早晨9点准时需要看到企业核心的经营数据,以便进行企业战略及方向决策 ...

  5. 2,Spring MVC 学习总结(二)- 方法(Action)参数映射

    一,Controller层方法(Action)参数映射 1,自动参数映射 1.1,基本数据类型参数映射 方法的参数可以是任意基本数据类型,如果方法参数名与http中请求的参数名称相同时会进行自动映射. ...

  6. 数字电路的与门、或门、非门--FPGA--005

    作者:young cc 链接:https://www.zhihu.com/question/23829080/answer/123791730来源:知乎著作权归作者所有,转载请联系作者获得授权. 数字 ...

  7. paper 167:GPU的使用Theano之tutorial

    Theano之使用GPU 英文版本:http://deeplearning.net/software/theano/tutorial/using_gpu.html          using the ...

  8. windows防火墙批量开放端口

    windows自带的防火墙没有增加端口范围的功能,这让一些常见的配置变得非常麻烦(比如FTP被动模式下,就需要在防火墙上开启端口范围),经过查询资料,使用以下cmd批处理内容,即可增加端口范围: ec ...

  9. [CSP-S模拟测试73]题解

    A.小P的2048 作为一个看B哥玩了一个寒假的人这种题闭眼切好吧 模拟即可.程序模块化后直接复制粘贴. 说什么模拟不能复制粘贴的都没水平 #include<cstdio> #includ ...

  10. 前端每日实战:36# 视频演示如何利用 CSS 动画原理,在页面上表现日蚀现象

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/OELvrK 可交互视频教程 此视频 ...