Special Binary String——LeetCode进阶路
原题链接https://leetcode.com/problems/special-binary-string/
题目描述
Special binary strings are binary strings with the following two properties:
The number of 0’s is equal to the number of 1’s.
字符0的个数与字符1的个数相等
Every prefix of the binary string has at least as many 1’s as 0’s.
二进制序列的每一个前缀码中 1 的数量要大于等于 0 的数量
Given a special string S, a move consists of choosing two consecutive, non-empty, special substrings of S, and swapping them. (Two strings are consecutive if the last character of the first string is exactly one index before the first character of the second string.)
At the end of any number of moves, what is the lexicographically largest resulting string possible?
Example 1:
Input: S = “11011000”
Output: “11100100”
Explanation:
The strings “10” [occuring at S[1]] and “1100” [at S[3]] are swapped.
This is the lexicographically largest string possible after some number of swaps.
Note:
S has length at most 50.
S is guaranteed to be a special binary string as defined above.
思路分析
给定二进制子串,在满足“特殊串”的两个要求得情况下,可以对子串进行任意次数的交换,返回字典序中最大的字符串。
第一直觉就是递归啦,对了一定要提看的大神的理解,爆炸赞,把问题看做括号字符串ヾ(゚∀゚ゞ)
1视为左括号,0视为右括号,特殊串的要求就变成括号必须能够配对,且在任意时刻左括号都要大于等于右括号~
eg : 11011000 ——> (() (()))
11100100 ——> ( (()) () )
要求是字典序最大,也就是1要尽量在前面,即左括号们尽量在前边
其实从样例也很直观,想让左括号们在前边,就需要在前边放那些嵌套层数多的。
注意:开头和结尾的那对括号是必须的,不可以改变,所以只要调整其内的子串即可。
嵌套问题当然找递归啦,最后排序得出结果,(o゜▽゜)o☆[BINGO!]
AC解
下面贴的是借鉴了大神后的优化版,我与大神的差距还隔着N个高级特性哇!
class Solution {
public String makeLargestSpecial(String S) {
if(S == null)
{
return null;
}
int count = 0;
List<String> res = new LinkedList<>();
for(int i=0,j=0; i<S.length() ; i++)
{
count = (S.charAt(i)=='1') ? count+1 : count-1;
if(count == 0)
{
res.add('1'+ makeLargestSpecial(S.substring(j+1,i)) +'0');
j = i + 1;
}
}
Collections.sort(res,Collections.reverseOrder());
return String.join("",res);
}
}


Special Binary String——LeetCode进阶路的更多相关文章
- [LeetCode] Special Binary String 特殊的二进制字符串
Special binary strings are binary strings with the following two properties: The number of 0's is eq ...
- 【LeetCode】761. Special Binary String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/special- ...
- leetcode 761. Special Binary String
761. Special Binary String 题意: 一个符合以下两个要求的二进制串: \(1.串中包含的1和0的个数是相等的.\) \(2.二进制串的所有前缀中1的个数不少于0的个数\) 被 ...
- [Swift]LeetCode761. 特殊的二进制序列 | Special Binary String
Special binary strings are binary strings with the following two properties: The number of 0's is eq ...
- 761. Special Binary String
Special binary strings are binary strings with the following two properties: The number of 0's is eq ...
- #Leetcode# 1016. Binary String With Substrings Representing 1 To N
https://leetcode.com/problems/binary-string-with-substrings-representing-1-to-n/ Given a binary stri ...
- 【LeetCode】1023. Binary String With Substrings Representing 1 To N 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- (String) leetcode 67. Add Binary
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- 【leetcode】1023. Binary String With Substrings Representing 1 To N
题目如下: Given a binary string S (a string consisting only of '0' and '1's) and a positive integer N, r ...
- [Swift]LeetCode1016. 子串能表示从 1 到 N 数字的二进制串 | Binary String With Substrings Representing 1 To N
Given a binary string S (a string consisting only of '0' and '1's) and a positive integer N, return ...
随机推荐
- Typecho弹出find Input author error怎么解决
大多数报错的起因就是因为,有些模板归档页面的评论功能被阉割掉了 于是导致一些评论验证功能的插件爆这个错误. #解决方法有以下三种 1,编辑这些没有评论功能的页面,高级设置,关闭评论,然后发布文章. 2 ...
- zabbix - [03] 安装部署
参考:https://www.yuque.com/fenghuo-tbnd9/ffmkvs zabbix6要求操作系统为Centos8,所以一开始安装部署的时候发现少了zabbix-server-my ...
- DeepSeek 开源周回顾「GitHub 热点速览」
上周,DeepSeek 发布的开源项目用一个词形容就是:榨干性能!由于篇幅有限,这里仅列出项目名称和简介,感兴趣的同学可以前往 DeepSeek 的开源组织页面,深入探索每个项目的精彩之处! 第一天 ...
- linux系统升级/更新OpenSSL版本操作流程记录
问题描述:有时OpenSSL版本过老升级,或者需要更新OpenSSL版本 1.登录linux系统后输入openssl version 查看现在使用的版本 我的输入后版本信息为:OpenSSL 1.1. ...
- 【Ryan】: linux下安装ftp
在 Linux 系统下安装 FTP 服务器可以使用多种软件,其中最常见的是 vsftpd(Very Secure FTP Daemon)和 ProFTPD(Professional FTP Daemo ...
- 对称&反对称&完全固定边界条件
ABAQUS Boundary Condition XSYMM 对称边界条件,对称面为与坐标轴1垂直的平面,即U1=UR2=UR3=0; YSYMM 对称边界条件,对称面为与坐标轴2垂直的平面,即U2 ...
- RealityCapture重建试验
一.使用已有数据集 (一)小型物件(官网) 输入:Camera_Lubitel2_studio "Lubitel Camera" consisting of 72 images 地 ...
- tsconfig.json 报错问题解决
tsconfig.json 报错问题解决 报错如图所示: 创建tsconfig.json配置文件时,VSCode会自动检测当前项目当中是否有ts文件,若没有则报错,提示用户需要创建一个ts文件后,再去 ...
- go sync.map的使用
前言 数据竞争是并发情况下,存在多线程/协程读写相同数据的情况,必须存在至少一方写.另外,全是读的情况下是不存在数据竞争的. Go语言中的 map 在并发情况下,只读是线程安全的,同时读写是线程不安全 ...
- 【Java】内部类详解
说起内部类这个词,想必很多人都不陌生,但是又会觉得不熟悉.原因是平时编写代码时可能用到的场景不多,用得最多的是在有事件监听的情况下,并且即使用到也很少去总结内部类的用法.今天我们就来一探究竟. 一.内 ...