当前位置:网站首页>927. 三等分 模拟
927. 三等分 模拟
2022-07-05 05:51:00 【钰娘娘】
927. 三等分
给定一个由
0和1组成的数组arr,将数组分成 3 个非空的部分 ,使得所有这些部分表示相同的二进制值。如果可以做到,请返回任何
[i, j],其中i+1 < j,这样一来:
arr[0], arr[1], ..., arr[i]为第一部分;arr[i + 1], arr[i + 2], ..., arr[j - 1]为第二部分;arr[j], arr[j + 1], ..., arr[arr.length - 1]为第三部分。- 这三个部分所表示的二进制值相等。
如果无法做到,就返回
[-1, -1]。注意,在考虑每个部分所表示的二进制时,应当将其看作一个整体。例如,
[1,1,0]表示十进制中的6,而不会是3。此外,前导零也是被允许的,所以[0,1,1]和[1,1]表示相同的值。示例 1:
输入:arr = [1,0,1,0,1] 输出:[0,3]示例 2:
输入:arr = [1,1,0,1,1] 输出:[-1,-1]示例 3:
输入:arr = [1,1,0,0,1] 输出:[0,2]提示:
3 <= arr.length <= 3 * 10^4arr[i]是0或1
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/three-equal-parts
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
做题结果
成功,此题需要比较清晰的模拟思路
方法:模拟
1. 0因为存在前导0,所以不能确定,但是1一定要能够均分为3份,此处可先判断。特别的全0返回,[0,n-1]
2. 尾部0的数目是由最后一个数的遇到最后一个1之后0的个数决定的,这些0不是前导0无法抹去
3. 有了尾部0的个数,有了1的个数,就可以分出3份了,最后比合成的三份是否全等即可
class Solution {
public int[] threeEqualParts(int[] arr) {
int cnt = 0;
for(int v:arr){
cnt += v;
}
int n = arr.length;
if(cnt == 0) return new int[]{0,n-1};
if(cnt%3!=0) return new int[]{-1,-1};
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
StringBuilder sb3 = new StringBuilder();
int zeroCnt = 0;
for(int i = n-1; i>=0;i--){
if(arr[i]==1){
zeroCnt = n-i-1;
break;
}
}
int curr = 0;
int endZero = 0;
int[] ans = new int[]{-1,-1};
for(int i = 0; i < n; i++){
if(curr <cnt/3||endZero<zeroCnt){
if(arr[i]==1){
sb1.append("1");
++curr;
endZero = 0;
}
else if(sb1.length()>0) {
sb1.append("0");
endZero++;
}
}else if(curr <cnt/3*2||endZero<zeroCnt*2){
if(ans[0]==-1) ans[0] = i-1;
if(arr[i]==1){
sb2.append("1");
++curr;
endZero = zeroCnt;
}
else if(sb2.length()>0){
sb2.append("0");
endZero++;
}
}else{
if(ans[1]==-1) ans[1] = i;
if(arr[i]==1){
sb3.append("1");
++curr;
}
else if(sb3.length()>0) sb3.append("0");
}
}
return sb1.toString().equals(sb2.toString()) && sb2.toString().equals(sb3.toString())?ans:new int[]{-1,-1};
}
}边栏推荐
- 对for(var i = 0;i < 5;i++) {setTimeout(() => console.log(i),1000)}的深入分析
- Sword finger offer 09 Implementing queues with two stacks
- Maximum number of "balloons"
- How many checks does kubedm series-01-preflight have
- R语言【数据集的导入导出】
- 【Rust 笔记】17-并发(上)
- 【Jailhouse 文章】Performance measurements for hypervisors on embedded ARM processors
- 中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
- ALU逻辑运算单元
- Using HashMap to realize simple cache
猜你喜欢

Light a light with stm32

Codeforces round 712 (Div. 2) d. 3-coloring (construction)

sync. Interpretation of mutex source code

Sword finger offer 53 - ii Missing numbers from 0 to n-1

Palindrome (csp-s-2021-palin) solution

Sword finger offer 53 - I. find the number I in the sorted array

Wazuh開源主機安全解决方案的簡介與使用體驗

【实战技能】如何做好技术培训?
![[cloud native] record of feign custom configuration of microservices](/img/39/05cf7673155954c90e75a8a2eecd96.jpg)
[cloud native] record of feign custom configuration of microservices

The connection and solution between the shortest Hamilton path and the traveling salesman problem
随机推荐
On the characteristics of technology entrepreneurs from Dijkstra's Turing Award speech
Wazuh開源主機安全解决方案的簡介與使用體驗
R语言【数据集的导入导出】
How to adjust bugs in general projects ----- take you through the whole process by hand
Codeforces round 712 (Div. 2) d. 3-coloring (construction)
Introduction et expérience de wazuh open source host Security Solution
Analysis of backdoor vulnerability in remote code execution penetration test / / phpstudy of national game title of national secondary vocational network security B module
Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
全国中职网络安全B模块之国赛题远程代码执行渗透测试 //PHPstudy的后门漏洞分析
After setting up the database and website When you open the app for testing, it shows that the server is being maintained
Convolution neural network -- convolution layer
Simply sort out the types of sockets
LaMDA 不可能觉醒吗?
SAP method of modifying system table data
【Rust 笔记】17-并发(上)
Typical use cases for knapsacks, queues, and stacks
Collection: programming related websites and books
剑指 Offer 53 - I. 在排序数组中查找数字 I
One question per day 1765 The highest point in the map
LeetCode 0108.将有序数组转换为二叉搜索树 - 数组中值为根,中值左右分别为左右子树