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 如何开启 HTTPS
一般来说,我们直接开启 HTTPS 就行,开启后进去网站后台修改网站的 URL 即可. 但是我昨天发现,我的工具箱迁移服务器之后,前台看着是很正常的,但是后台的登陆页面引入的依然的 http 标头,所 ...
- 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
从 Chrome 125 开始,支持了一个全新的 CSS 特性 - Anchor Positioning,翻译过来即是锚点定位. 在之前的文章中,我们较为系统的讲述了这个新特性的使用,感兴趣的可以翻开 ...
- 961. 重复 N 次的元素
地址:https://leetcode-cn.com/problems/n-repeated-element-in-size-2n-array/ <?php /** 在大小为 2N 的数组 A ...
- 面试题55 - I. 二叉树的深度
地址:https://leetcode-cn.com/problems/er-cha-shu-de-shen-du-lcof/ <?php /** 面试题55 - I. 二叉树的深度 输入一棵二 ...
- pikachu搭建
pikachu靶场搭建教程 下载小皮面板phpstudy: 小皮官网:https://www.xp.cn/ 下载pikachu : https://github.com/zhuifengshaonia ...
- C# 中比较实用的关键字,基础高频面试题!
前言 在C#编程中关键字是构建逻辑和实现功能的基石,它承载着编程语言的语法规则和编程智慧.熟练掌握这些基础高频关键字对提升编程能力和面试表现至关重要,它们是日常开发和解决复杂问题的关键. DotNet ...
- 权限获得第一步-NTLM暴力破解
题目: 你猜这是什么东西,记得破解后把其中的密码给我.答案为非常规形式. Administrator:500:806EDC27AA52E314AAD3B435B51404EE:F4AD50F57683 ...
- Delphi 判断字符是否是汉字
function IsHZ(ch: WideChar): boolean; var i: Integer; begin i := Ord(ch); if (i < 19968) or (i &g ...
- CSAPP学习笔记——chapter4 处理器体系结构
CSAPP学习笔记--chapter4 处理器体系结构 这一章相对于其它的章节,是相对来说比较困难的一章:其它章节的一些内容都在计组,计网,操作系统等课程里面已经接触过一些概念,但是有关处理器,我才发 ...
- markdown常用命令行格式
Markdown 主要命令(语法)如下: 标题 使用 # 号表示标题,# 的个数决定标题的级别: 一级标题 二级标题 三级标题 四级标题 五级标题 六级标题 段落 & 换行 直接输入文字形成段 ...