761. Special Binary String

题意:

一个符合以下两个要求的二进制串:

\(1.串中包含的1和0的个数是相等的。\)

\(2.二进制串的所有前缀中1的个数不少于0的个数\)

被称为特殊二进制串

要求我们任意交换两个相邻的特殊二进制串(可以交换任意次)使得最终得到的序列的字典序最大,并且满足是特殊二进制串。

思路:

我们可以将1看作是上升,0是下降,那么可以这样

然后我们可以找最外层符合要求的子串也就是最底部从水平线开始又降到水平线的段,可以发现找这样的串只要统计一个cnt变量,然后cnt第一次从1变到0就可以找到一段。我们发现这些端除去首首尾,‘1’+str+‘0’,那么我们证明str是以1开头和0结尾的特殊串假设str以1结尾那么‘1’+str 的cnt计数为1,那么去除str最后的1,cnt = 0,那么与选择的串为第一个cnt=0不符,那么假设str首个为0,那么10为第一个cnt = 0所选的字段与当前不符,又前特殊串前后剥去1,0不改变特殊性。

那些段就可以看成是原问题的一个子问题,所以我们就可以递归处理了。由于交换特殊串的时候串的前缀和始终满足要求,所以只要sort下就可以了。

题链

代码:

class Solution
{
public:
string makeLargestSpecial(string S)
{
int len = S.length();
int cnt = 0;
vector<string>str;
int pr = 0;
for(int i = 0;i < len ;i++)
{
if(S[i] == '1')
cnt++;
else cnt--;
if(cnt==0)
{ //printf("%d\n",i);
str.push_back('1' + makeLargestSpecial(S.substr(pr+1,i-pr)) + '0');
pr = i+1;
} }
sort(str.begin(),str.end(),cmp);
string ask = "";
for(int i = 0;i < str.size();i++)
ask += str[i];
return ask;
}
static bool cmp(string a,string b)
{
return a > b;
}
};

leetcode 761. Special Binary String的更多相关文章

  1. 【LeetCode】761. Special Binary String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/special- ...

  2. 761. Special Binary String

    Special binary strings are binary strings with the following two properties: The number of 0's is eq ...

  3. [LeetCode] Special Binary String 特殊的二进制字符串

    Special binary strings are binary strings with the following two properties: The number of 0's is eq ...

  4. [Swift]LeetCode761. 特殊的二进制序列 | Special Binary String

    Special binary strings are binary strings with the following two properties: The number of 0's is eq ...

  5. 【LeetCode】1023. Binary String With Substrings Representing 1 To N 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 【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 ...

  7. #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 ...

  8. (String) leetcode 67. Add Binary

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

  9. LeetCode 面试:Add Binary

    1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...

随机推荐

  1. JVM1 JVM与Java体系结构

    目录 JVM与Java体系结构 虚拟机与Java虚拟机 虚拟机 Java虚拟机 JVM的位置 JVM的整体结构 Java代码执行流程 JVM的架构模型 基于栈的指令级架构 基于寄存器的指令级架构 两种 ...

  2. 日常Java 2021/10/25

    ArrayList存储数字 import java.util.ArrayList; public class Arr_test { public static void main(String[] a ...

  3. Android Bitmap 全面解析(一)加载大尺寸图片

    压缩原因:1.imageview大小如果是200*300那么加载个2000*3000的图片到内存中显然是浪费可耻滴行为;2.最重要的是图片过大时直接加载原图会造成OOM异常(out of memory ...

  4. [学习总结]5、Android的ViewGroup中事件的传递机制(二)

    下面是第一篇的连接 Android的ViewGroup中事件的传递机制(一) 关于onInterceptTouchEvent和onTouchEvent的详细解释. 1 public class Mai ...

  5. Use of explicit keyword in C++

    Predict the output of following C++ program. 1 #include <iostream> 2 3 using namespace std; 4 ...

  6. Copy constructor vs assignment operator in C++

    Difficulty Level: Rookie Consider the following C++ program. 1 #include<iostream> 2 #include&l ...

  7. Android: EditText设置属性和设置输入规则

    1.EditText输入限制规则 在xml:EditText 设置属性 android:digits="ABCDE123&*" ABCDE123&*是你的限制规则 ...

  8. shell脚本采集系统cpu、内存、磁盘、网络信息

    有不少朋友不知道如何用shell脚本采集linux系统相关信息,包括cpu.内存.磁盘.网络等信息,这里脚本小编做下讲解,大家一起来看看吧. 一.cpu信息采集 1),采集cpu使用率采集算法:通过/ ...

  9. jQuery - focusin/focusout/focus/blur事件的区别与不同

    focus与blur事件:不支持冒泡 focusin与focusout:支持冒泡 事件触发顺序: 对于同时支持这4个事件的浏览器,事件执行顺序为focusin(聚焦) > focus > ...

  10. 1.RabbitMQ

    1.RabbitMq是什么?    MQ全称为Message Queue,即消息队列, RabbitMQ是由erlang语言开发,基于AMQP(Advanced Message Queue 高级消息队 ...