题目如下:

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. hdu 5964:平行四边形 【计算几何】

    打重现赛时,一点思路也没有,然后又看到这题AC数那么少,就直接放弃了.今天重新看了看,借鉴了下别人的,发现此题应该算是一道可解题. 看上去,这题的ans是同时有两个点作为自变量的函数(然而n^2复杂度 ...

  2. Oracle11g新建用户及用户表空间

    /* 建立数据表空间 */CREATE TABLESPACE SP_TAB DATAFILE '/u01/app/oracle/oradata/orcl/tab1_1.dbf' size 1024M ...

  3. 2.Javascript 函数(主要)

    定义函数 在JavaScript中,定义函数的方式如下: function abs(x) { if (x >= 0) { return x; } else { return -x; } } 上述 ...

  4. dumpsys, traceView调试命令

    1. dumpsys dumpsys cpuinfo: 打印cpu使用情况: dumpsys meminfo: 打印内存使用率情况: dumpsys activity: 打印所有活动的信息: dump ...

  5. LG2704 [NOI2001] 炮兵阵地

    题目描述 (试题来源:Link ) 司令部的将军们打算在 \(N\times M\) 的网格地图上部署他们的炮兵部队.一个 \(N\times M\) 的地图由 \(N\) 行 \(M\) 列组成,地 ...

  6. Spring CGLlB动态代理

    JDK 动态代理使用起来非常简单,但是它也有一定的局限性,这是因为 JDK 动态代理必须要实现一个或多个接口,如果不希望实现接口,则可以使用 CGLIB 代理. CGLIB(Code Generati ...

  7. css > 的写法 html

    .userInfo-view .info .name::after { content: " "; display: inline-block; height: 12rpx; wi ...

  8. Windows-菜单太长点不到

    显示设置里显示器方向调整成纵向

  9. css缓存问题

    频繁更换样式,会导致样式缓存, 在实际项目开发过过程中,页面是上传到服务器上的.而为了减少服务器的压力,让用户少加载,浏览器会将图片.css.js缓存到本地中,以便下次访问网站时使用.这样做不仅减少了 ...

  10. 斯坦福【概率与统计】课程笔记(三):EDA | 直方图

    单个定量变量的直方图表示 大家知道,定量变量是连续型变量,即不会像分类变量那样有明显的分类,那么如何将其画成直方图呢?一般来说,会将其按照某个维度来将其分组(group),举个例子. 我们有15个学生 ...