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 ...
随机推荐
- burpsuite激活
激活burpsuite--教程 点击Start 文件,把三个框都选上 点击RUN,会自动启动,复制一下那个证书 粘贴刚刚复制的密钥,点击下一个即可 这里点击手动激活,复制请求,粘贴到刚刚那个激活程序的 ...
- 【多进程并发笔记】Python-Multiprocess
目录 调用函数后,函数内的变量如何释放? python2.7怎么使用多线程加速for loop 多进程进程池,函数序列化错误的处理 Time模块计算程序运行时间 使用多进程,Start()后,如何获得 ...
- grpc unable to determine Go import path for
前言 在 proto 文件夹下执行如下命令: $ protoc --go_out=plugins=grpc:. *.proto 报错:无法确定Go导入路径 protoc-gen-go: unable ...
- hotmail 获取邮箱授权码
第一步:登录microsoft账户,进入到安全性页面. https://account.microsoft.com/?lang=zh-CN&refd=account.live.com& ...
- Centos双网卡冗余绑定
1. 前言 关于双网卡绑定,前前后后踩过不少的坑,虽然这是 RHCE 中的一道题,但是在实践中碰到问题也够喝一壶的. 在实践中,虚拟机.物理机都做过,但是不尽相同,大部分的坑也集中在这里,本文长期更新 ...
- Windows下Dll在Unity中使用的一般方式
Windows下Dll在Unity中使用的一般方式 Unity中虽然已经有广泛的库和插件,但是相较于C++的库生态而言,还是有一定的差距:因此本篇博文记录Windows下将C++函数打包成动态链接库在 ...
- Oracle impdp 导入报错 ORA-39083 + ORA-00439
Oracle 11G R2 impdp导入的时候 一直报错: ORA-39083: 对象类型 TABLE:"xxx"."xxx" 创建失败, 出现错误: ORA ...
- Go 应用程序使用 dockerfile multi-stage 的问题
场景重现 一个简单的go应用,准备通过docker部署,为了减少运行时的镜像和容器体积,使用了multi-stage构建: # dockerfile 大致如下 # 一级构建使用带golang环境的镜像 ...
- 学习Kotlin语法(三)
简介 在上一节,我们对Kotlin中面向对象编程(OOP)的相关知识有了大致的了解,本章节我们将去进一步了解函数.lambada表达式.内联函数.操作符重载.作用域函数. 目录 函数 函数的使用 参数 ...
- C# LINQ 快速入门实战指南,建议收藏学习!
前言 因为咱们的.NET EF Core快速入门实战教程经常会用到 LINQ 去查询和操作 MySQL 中的数据,因此我觉得很有必要对 LINQ 的一些使用技巧.常用方法.特性做一个详细的介绍,让大家 ...