leetcode 761. Special Binary String
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的更多相关文章
- 【LeetCode】761. Special Binary String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/special- ...
- 761. Special Binary String
Special binary strings are binary strings with the following two properties: The number of 0's is eq ...
- [LeetCode] Special Binary String 特殊的二进制字符串
Special binary strings are binary strings with the following two properties: The number of 0's is eq ...
- [Swift]LeetCode761. 特殊的二进制序列 | Special Binary String
Special binary strings are binary strings with the following two properties: The number of 0's is eq ...
- 【LeetCode】1023. Binary String With Substrings Representing 1 To N 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【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 ...
- #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 ...
- (String) leetcode 67. Add Binary
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- LeetCode 面试:Add Binary
1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...
随机推荐
- 硬盘SSD、HDD和SSHD都是什么意思?哪种类型硬盘最好?
硬盘分类:(1)HHD 机械硬盘(Mechanical hard disk)(2)SSD 固态硬盘(solid state drive/disk)(3)SSHD 混合硬盘,说白了就是HDD+SSD=S ...
- 学习java的第十七天
一.今日收获 1.java完全学习手册第三章算法的3.1比较值 2.看哔哩哔哩上的教学视频 二.今日问题 1.在第一个最大值程序运行时经常报错. 2.哔哩哔哩教学视频的一些术语不太理解,还需要了解 三 ...
- 面向多场景而设计的 Erda Pipeline
作者|林俊(万念) 来源|尔达 Erda 公众号 Erda Pipeline 是端点自研.用 Go 编写的一款企业级流水线服务.截至目前,已经为众多行业头部客户提供交付和稳定的服务. 为什么我们坚持自 ...
- Linux基础命令---ntpq查询时间服务器
ntpq ntpq指令使用NTP模式6数据包与NTP服务器通信,能够在允许的网络上查询的兼容的服务器.它以交互模式运行,或者通过命令行参数运行. 此命令的适用范围:RedHat.RHEL.Ubuntu ...
- Linux基础命令---lynx浏览器
lynx lynx是一个字符界面的全功能www浏览器,它没有图形界面,因此占用的资源较少. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora. 1.语法 ...
- mysql触发器实例说明
触发器是一类特殊的事务 ,可以监视某种数据操作(insert/update/delete),并触发相关操作(insert/update/delete). 看以下事件: 完成下单与减少库存的逻辑 Ins ...
- feignclient发送get请求,传递参数为对象
feignclient发送get请求,传递参数为对象.此时不能使用在地址栏传递参数的方式,需要将参数放到请求体中. 第一步: 修改application.yml中配置feign发送请求使用apache ...
- MyBatis绑定Mapper接口参数到Mapper映射文件sql语句参数
一.设置paramterType 1.类型为基本类型 a.代码示例 映射文件: <select id="findShopCartInfoById" parameterType ...
- 30个类手写Spring核心原理之依赖注入功能(3)
本文节选自<Spring 5核心原理> 在之前的源码分析中我们已经了解到,依赖注入(DI)的入口是getBean()方法,前面的IoC手写部分基本流程已通.先在GPApplicationC ...
- SQL->Python->PySpark计算KS,AUC及PSI
KS,AUC 和 PSI 是风控算法中最常计算的几个指标,本文记录了多种工具计算这些指标的方法. 生成本文的测试数据: import pandas as pd import numpy as np i ...