Files
2020-08-09 00:39:24 +08:00

58 lines
2.6 KiB
Markdown
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# [927. Three Equal Parts](https://leetcode.com/problems/three-equal-parts/)
## 题目
Given an array `A` of `0`s and `1`s, 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`
## 题目大意
给定一个由 0 和 1 组成的数组 A将数组分成 3 个非空的部分使得所有这些部分表示相同的二进制值。如果可以做到请返回任何 [i, j],其中 i+1 < j这样一来
- A[0], A[1], ..., A[i] 组成第一部分
- A[i+1], A[i+2], ..., A[j-1] 作为第二部分
- A[j], A[j+1], ..., A[A.length - 1] 是第三部分
- 这三个部分所表示的二进制值相等
如果无法做到就返回 [-1, -1]。
注意在考虑每个部分所表示的二进制时应当将其看作一个整体例如[1,1,0] 表示十进制中的 6而不会是 3此外前导零也是被允许的所以 [0,1,1]  [1,1] 表示相同的值
提示
1. 3 <= A.length <= 30000
2. A[i] == 0 A[i] == 1
## 解题思路
- 给出一个数组数组里面只包含 0 1要求找到 2 个分割点使得分成的 3 个子数组的二进制是完全一样的
- 这一题的解题思路不难按照题意模拟即可先统计 1 的个数 total然后除以 3 就是每段 1 出现的个数有一些特殊情况需要额外判断一下例如没有 1 的情况那么只能首尾分割1 个个数不是 3 的倍数也无法分割成满足题意然后找到第一个 1 的下标然后根据 total/3 找到 mid第一个分割点再往后移动找到第二个分割点找到这 3 个点以后同步的移动这 3 个点移动中判断这 3 个下标对应的数值是否相等如果都相等并且最后一个点能移动到末尾就算找到了满足题意的解了