【leetcode】927. Three Equal Parts
题目如下:
Given an array
Aof0s and1s, 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]withi+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, andA[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]represents6in decimal, not3. 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:
3 <= A.length <= 30000A[i] == 0orA[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的更多相关文章
- 【LeetCode】416. Partition Equal Subset Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 动态规划 日期 题目地址:https://l ...
- 【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 ...
- 【leetcode】1224. Maximum Equal Frequency
题目如下: Given an array nums of positive integers, return the longest possible length of an array prefi ...
- 【leetcode】416. Partition Equal Subset Sum
题目如下: 解题思路:对于这种判断是否的题目,首先看看动态规划能不能解决.本题可以看成是从nums中任选i个元素,判断其和是否为sum(nums)/2,很显然从nums中任选i个元素的和的取值范围是[ ...
- 【leetcode】698. Partition to K Equal Sum Subsets
题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...
- 【leetcode】486. Predict the Winner
题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...
- 【leetcode】668. Kth Smallest Number in Multiplication Table
题目如下: 解题思路:几乎和[leetcode]719. Find K-th Smallest Pair Distance 的方法一样.只不过一个是减法一个是乘法,还有一点区别是[leetcode]7 ...
- 【LeetCode】760. Find Anagram Mappings 解题报告
[LeetCode]760. Find Anagram Mappings 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/find ...
- 【LeetCode】299. Bulls and Cows 解题报告(Python)
[LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
随机推荐
- Install packages failed: Installing packages: error occurred. ------简单的问题常常会被忽略
用 pip install 安装了wxpy这个库,但是使用的时候却报错:ImportError: No module named wxpy 我先用 pip list 查看了一下,发现这个库是已经存在的 ...
- mybatis源码分析之01环境搭建
直接使用maven搭建一个mybatis的运行环境 1. pom.xml <?xml version="1.0" encoding="UTF-8"?> ...
- 4,JPA
一,什么是JPA JPA全称Java Persistence API.JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中. JPA(Java Pers ...
- bzoj4397【Usaco2015 Dec】Breed Counting(前缀和、树状数组)
题目描述 Farmer John's N cows, conveniently numbered 1…N, are all standing in a row (they seem to do so ...
- Android逆向之旅---Android应用的汉化功能(修改SO中的字符串内容)
一.前言 今天我们继续来讲述逆向的知识,今天我们来讲什么呢?我们在前一篇文章中介绍了关于SO文件的格式,今天我们继续这个话题来看看如何修改SO文件中的内容,看一下我们研究的主题: 需求:想汉化一个Ap ...
- Centos7.4 配置之MySQL 8.0【转】
首先查看Mysql最新版本, 此时,目前最新版本为8.0. 开始安装前需要一些准备工作. 1,将本地的MariaDB或者已经安装的MySQL其他版本卸载. (一)卸载本地的本地的MariaDB: 由于 ...
- QTP笔记--检查点、ChildObjects方法
一.自带检查点函数 '最后一个参数为timeout,单位是毫秒 Browser( "SAP NetWeaver Portal" ).Page( "SAP NetWeave ...
- 信息安全-攻击-XSRF:XSRF/CSRF 攻击
ylbtech-信息安全-攻击-XSRF:XSRF/CSRF 攻击 CSRF(Cross-site request forgery)跨站请求伪造,也被称为“One Click Attack”或者Ses ...
- Asp.Net Core 第05局:读取配置
前言 本文介绍Asp.Net Core 读取配置文件. 环境 1.Visual Studio 2017 2.Asp.Net Core 2.2 开局 前期准备 1.添加app.j ...
- 获取header中content-type的值
后台传过来的值需要根据content-Type的值来判定成功与否 获取header中content-Tyep的值 用res.header['Content-Type']