当前位置:网站首页>【LeetCode】761.特殊的二进制序列
【LeetCode】761.特殊的二进制序列
2022-08-09 04:55:00 【酥酥~】
题目
特殊的二进制序列是具有以下两个性质的二进制序列:
0 的数量与 1 的数量相等。
二进制序列的每一个前缀码中 1 的数量要大于等于 0 的数量。
给定一个特殊的二进制序列 S,以字符串形式表示。定义一个操作 为首先选择 S 的两个连续且非空的特殊的子串,然后将它们交换。(两个子串为连续的当且仅当第一个子串的最后一个字符恰好为第二个子串的第一个字符的前一个字符。)
在任意次数的操作之后,交换后的字符串按照字典序排列的最大的结果是什么?
示例 1:
输入: S = “11011000”
输出: “11100100”
解释:
将子串 “10” (在S[1]出现) 和 “1100” (在S[3]出现)进行交换。
这是在进行若干次操作后按字典序排列最大的结果。
说明:
S 的长度不超过 50。
S 保证为一个满足上述定义的特殊 的二进制序列。
题解
评价:题都看不懂,逻辑不懂。看了题解才知道题是什么意思
这里的前缀码容易误导人,理解为前缀比较合适,<=n-1的前缀
主要就是理解特殊二进制序列:
- 1和0数目相等
- 1开始,0结尾
- 类似括号,必须“(”开始,“)”结尾
class Solution {
public:
string makeLargestSpecial(string s) {
int len = s.size();
if(len<=2)
return s;
vector<string> myvec;
int cnt = 0;
int pre = 0;
for(int i=0;i<len;i++)
{
if(s[i] == '1')
{
cnt++;
}
else
{
cnt--;
if(cnt==0)
{
myvec.push_back("1" + makeLargestSpecial(s.substr(pre + 1, i - pre - 1)) + "0");
pre = i+1;
}
}
}
sort(myvec.begin(),myvec.end(),greater<string>{
});
// for(auto ss:myvec)
// cout<<ss<<" ";
return accumulate(myvec.begin(),myvec.end(),string(""));
}
};
边栏推荐
猜你喜欢
必须指定GDAL API版本。提供一个路径使用GDAL_CONFIG gdal-config环境
Why do enterprises need business intelligence BI in the digital age
Ali YunTianChi competition problem (machine learning) - O2O coupons prediction (complete code)
后台登录模块以及验证码登录
C进阶 - 程序的编译(预处理操作) + 链接
还不了解什么是商业智能(BI)?看完这篇文章就懂了
Alibaba Cloud Tianchi Contest Question (Machine Learning) - Prediction of Industrial Steam Volume (Complete Code)
The influence law of genes for disease - read the paper
【ITRA】2022年ITRA赛事注册流程 从0-1
leetcode:316. 去除重复字母
随机推荐
FastDFS快速使用及介绍
抖音直播新号怎么起号?抖音直播间不进人怎么办?
dsafasfdasfasf
dsafasfdasfasf
C进阶 - 程序的编译(预处理操作) + 链接
php使用phpoffice/phpspreadsheet导入导出excel表格
【学习笔记】AGC044
【luogu U142356】Suffix of the Brave (SA) (Chairman Tree) (2 points)
could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarExcept
【Harmony OS】【ARK UI】轻量级数据存储
mysql content does not exist error
Nacos源码安装
还不了解什么是商业智能(BI)?看完这篇文章就懂了
如何剪裁svg并压缩
I.MX6U-ALPHA开发板(高精度定时器)
JS-全局dom对象的使用---使用htm样式和js函数动作的完全分离
JS-DOM--全局DOM对象document-history-location-navigator-screen-window
pytorch implementation of Poly1CrossEntropyLoss
JS-DOM-对象的事件onload、匿名函数、this
OKR management process, how to implement effective dialogue, using the CFR feedback and recognition?