题目如下:

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. JavaWeb(六):会话与状态管理

    HTTP协议是一种无状态的协议,WEB服务器本身不能识别出哪些请求是同一个浏览器发出的 ,浏览器的每一次请求都是完全孤立的.即使 HTTP1.1 支持持续连接,但当用户有一段时间没有提交请求,连接也会 ...

  2. Python的pip源切换为国内阿里云镜像

    Python的pip源切换为国内阿里云镜像 找到用户目录 C:\Users\用户\pip,如果不存在就新建该文件夹. 新建文件pip.ini,并用文本编辑器输入以下内容并保存 [global] ind ...

  3. java-设计原则

    七大设计原则 单一职责原则: 尽可能的功能细分(类细分,方法细分):如一个类由于某变量而细分方法,该细分方法再细分,需要重构(最好细分类) 接口隔离原则:(C类实现A接口全部方法,而D,B类依赖于A接 ...

  4. Apache Flink 进阶(八):详解 Metrics 原理与实战

    本文由 Apache Flink Contributor 刘彪分享,本文对两大问题进行了详细的介绍,即什么是 Metrics.如何使用 Metrics,并对 Metrics 监控实战进行解释说明. 什 ...

  5. 集训队8月9日(组合计数+容斥原理+Mobius函数)

    刷题数:4 今天看了组合计数+容斥原理+Mobius函数,算法竞赛进阶指南169~179页 组合计数 https://www.cnblogs.com/2462478392Lee/p/11328938. ...

  6. nginx启动、关闭、重启及常用的命令

    nginx常用命令 启动:cd /usr/local/nginx/sbin./nginxnginx服务启动后默认的进程号会放在/usr/local/nginx/logs/nginx.pid文件cat ...

  7. spring cloud stream集成rabbitmq

    pom添加依赖 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId&g ...

  8. 洛谷P3366 【模板】最小生成树(LCT)

    [模板]最小生成树 题目传送门 解题思路 用LCT来维护最小生成树. 除了把各顶点作为节点外,每条边也都视为一个节点.对于要加入的边\(e\),检查其两顶点\(x\)和\(y\)是否在同一棵树中,如果 ...

  9. 【awk】 处理多个文件

    处理多个文件: 1. 可以在代码中指定读取某个文件, 其他的用命令行输入           while ( geline < "file.txt" > 0 ) {   ...

  10. Selenium:八种元素定位方法

    前言: 我们在做WEB自动化时,最根本的就是操作页面上的元素,首先我们要能找到这些元素,然后才能操作这些元素.工具或代码无法像我们测试人员一样用肉眼来分辨页面上的元素.那么我们怎么来定位他们呢? 在学 ...