当前位置:网站首页>【899. 有序队列】
【899. 有序队列】
2022-08-03 15:23:00 【千北@】
来源:力扣(LeetCode)
描述:
给定一个字符串 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 <= 1000s只由小写字母组成。
方法:分情况讨论


代码:
class Solution {
public:
string orderlyQueue(string s, int k) {
if (k == 1) {
string smallest = s;
int n = s.size();
for (int i = 1; i < n; i++) {
char c = s[0];
s = s.substr(1);
s.push_back(c);
if (s < smallest) {
smallest = s;
}
}
return smallest;
} else {
sort(s.begin(), s.end());
return s;
}
}
};
执行用时:4 ms, 在所有 C++ 提交中击败了74.17%的用户
内存消耗:11.5 MB, 在所有 C++ 提交中击败了20.53%的用户
author:LeetCode-Solution
边栏推荐
- C#.NET 国密数字信封
- How much does Ark Survival Evolved cost?
- 上亿数据怎么玩深度分页?兼容MySQL + ES + MongoDB
- cmd 关闭端口
- 问题5:发现缺陷怎么办?缺陷的类型有哪些?
- 2021年12月电子学会图形化四级编程题解析含答案:质数判断器
- 测试基础整合-测试分类、软件质量模型、测试流程、测试用例、测试点划分方法、缺陷、例子
- 您的移动端app安全吗
- How to use redis
- The general trend, another key industry related to Sino-US competition, has reached a critical moment
猜你喜欢

STM32H743VIT6配置ADC为1M采样率

方舟生存进化开服需要多少钱
![[The Beauty of Software Engineering - Column Notes] 36 | What exactly do DevOps engineers do?](/img/7e/405a32a67ba48342179bfbcb214f09.png)
[The Beauty of Software Engineering - Column Notes] 36 | What exactly do DevOps engineers do?

16 【过渡 动画】

问题5:发现缺陷怎么办?缺陷的类型有哪些?

redis的使用方法

MMA安装及使用优化

After the cnpm installation is successful, the prompt is not an internal and external command, nor is it a runnable command solution

cmd 关闭端口

技术分享 | 接口自动化测试如何搞定 json 响应断言?
随机推荐
冒烟测试冒烟测试
Ark server open tool, server tutorial win
2022年镇海夏令营组合数学和数论班 —— 数学作业 1
语音识别新一轮竞争打响,自然对话会是下一个制高点吗?
devops-2:Jenkins的使用及Pipeline语法讲解
2021年12月电子学会图形化三级编程题解析含答案:跳高比赛
AWS中国区SDN Connector
身为程序员的我们如何卷死别人?破局重生。
简介undo log、truncate、以及undo log如何帮你回滚事物?
上亿数据怎么玩深度分页?兼容MySQL + ES + MongoDB
How to prevent hacking Windows server security Settings
使用虚幻引擎自动化工具实现自动化部署
彻底搞懂云桌面配置及实践踩坑【华为云至简致远】
After the cnpm installation is successful, the prompt is not an internal and external command, nor is it a runnable command solution
DeepLink在转转的实践
问题4:什么是缺陷?你们公司缺陷的优先级是怎样划分的?
Use Typora+EasyBlogImageForTypora to write a blog and upload pictures quickly without a picture bed
Several methods of installing Mysql in Linux
C#.NET 国密数字信封
0 code 4 steps to experience IoT devices on the cloud
