当前位置:网站首页>【LeetCode】899.有序队列
【LeetCode】899.有序队列
2022-08-04 10:50:00 【酥酥~】
题目
给定一个字符串 s 和一个整数 k 。你可以从 s 的前 k 个字母中选择一个,并把它加到字符串的末尾。
返回 在应用上述步骤的任意数量的移动后,字典上最小的字符串 。
示例 1:
输入:s = “cba”, k = 1
输出:“acb”
解释:
在第一步中,我们将第一个字符(“c”)移动到最后,获得字符串 “bac”。
在第二步中,我们将第一个字符(“b”)移动到最后,获得最终结果 “acb”。
示例 2:
输入:s = “baaca”, k = 3
输出:“aaabc”
解释:
在第一步中,我们将第一个字符(“b”)移动到最后,获得字符串 “aacab”。
在第二步中,我们将第三个字符(“c”)移动到最后,获得最终结果 “aaabc”。
提示:
1 <= k <= S.length <= 1000
s 只由小写字母组成。
题解
当k == 1时,只能将第一个元素移到尾部,有n种可能,结果就是这n种可能中最小的字符串
当k > 1时,则字符串一定能排序成一个有序队列,结果就是一个升序的有序队列
class Solution {
public:
string orderlyQueue(string s, int k) {
if(k == 1)
{
string result = s;
int len = s.length();
for(int i=0;i<len;i++)
{
s = s.substr(1)+s[0];
result = min(result,s);
}
return result;
}
sort(s.begin(),s.end());
return s;
}
};
边栏推荐
- 8月活动|51CTO十七周年庆,发博文得茶具/笔记本/T恤等礼品!
- MySQL最大建议行数2000w, 靠谱吗?
- 使用.NET简单实现一个Redis的高性能克隆版(二)
- Introduction to the core methods of the CompletableFuture interface
- Super Learning Method
- winform 在Datatable插入一笔数据
- Camunda整体架构和相关概念
- C language * Xiaobai's adventure
- Servlet基础详细版
- Meishe Q&A Room | Meiying VS Meishe Cloud Editing
猜你喜欢

美摄问答室|美映 VS 美摄云剪辑
![[论文阅读] Unpaired Image-to-Image Translation Using Adversarial Consistency Loss](/img/4d/9c2f94f475834771f6ad6ffe8f8b35.png)
[论文阅读] Unpaired Image-to-Image Translation Using Adversarial Consistency Loss

A topic of map

Camunda整体架构和相关概念

Events in August | 51CTO's 17th Anniversary Celebration, post a blog post to get gifts such as tea sets/notebooks/T-shirts!

Use pytest hook function to realize automatic test result push enterprise WeChat

HCIP 第十八天

语音社交app源码——具备哪些开发优势?

zabbix部署

【励志】复盘的重要性
随机推荐
《迁移学习导论》第2版,升级内容抢先看!
iMeta | 德国国家肿瘤中心顾祖光发表复杂热图(ComplexHeatmap)可视化方法
ORB-SLAM3中的优化
线程必备内容
开源一夏|ArkUI如何自定义弹窗(eTS)
Meishe Q&A Room | Meiying VS Meishe Cloud Editing
[论文翻译] Unpaired Image-to-Image Translation using Adversarial Consistency Loss
【Inspirational】The importance of review
Jenkins User Manual (1) - Software Installation
Apache Calcite 框架原理入门和生产应用
Maple 2022软件安装包下载及安装教程
Mobile open source low code tools beeware and kivy
SkiaSharp 之 WPF 自绘 粒子花园(案例版)
高级转录组分析和R数据可视化火热报名中(2022.10)
【Idea series】idea configuration
js文字转语音播报
iMeta | Baidu certification is completed, search "iMeta" directly to the publisher's homepage and submission link
从零开始Blazor Server(7)--使用Furion权限验证
MySQL核心SQL:结构化查询语句SQL、库操作、表操作、CRUD
什么是终端特权管理