当前位置:网站首页>LeetCode 899. 有序队列
LeetCode 899. 有序队列
2022-08-03 19:52:00 【JIeJaitt】
给定一个字符串 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 res = s;
for(int i=1;i<s.size();i++) {
char c=s[0];
s=s.substr(1);
s.push_back(c);
if(s<res) {
res = s;
}
}
return res;
} else {
sort(s.begin(),s.end());
return s;
}
}
};
class Solution {
public:
string orderlyQueue(string s, int k) {
if (k==1) {
string res = s;
for(int i=1;i<s.size();i++) {
s=s.substr(1)+s[0];
res=min(res,s);
}
return res;
}
sort(s.begin(),s.end());
return s;
}
};
func orderlyQueue(s string, k int) string {
if k==1 {
res := s
for i:=1;i<len(s);i++ {
s=s[1:]+s[:1]
if s<res {
res=s
}
}
return res;
}
t := []byte(s)
sort.Slice(t,func(i,j int) bool {
return t[i]<t[j]})
return string(t)
}
边栏推荐
猜你喜欢

高位套牢机构,用友网络的信任危机是如何产生的?

盘点在线帮助中心对企业能够起到的作用

【飞控开发高级教程4】疯壳·开源编队无人机-360 度翻滚

Jingdong cloud released a new generation of distributed database StarDB 5.0

ECCV 2022 Oral | 满分论文!视频实例分割新SOTA: IDOL

Statistical machine learning 】 【 linear regression model

消除对特权账户的依赖使用Kaniko构建镜像

Handler source code analysis

Golang死信队列的使用
Teach you to locate online MySQL slow query problem hand by hand, package teaching package meeting
随机推荐
FreeRTOS中级篇
ThreadLocal详解
群辉查看硬盘存储占用的方式
149. 直线上最多的点数-并查集做法
The effective square of the test (one question of the day 7/29)
余弦距离介绍
622 设计循环队列——Leetcode天天刷【循环队列,数组模拟,双指针】(2022.8.2)
Matlab论文插图绘制模板第42期—气泡矩阵图(相关系数矩阵图)
倒计时2天,“文化数字化战略新型基础设施暨文化艺术链生态建设发布会”启幕在即
LeetCode 622. Designing Circular Queues
Postgresql source code (65) analysis of the working principle of the new snapshot system Globalvis
【STM32】标准库-自定义BootLoader
1161 最大层内元素和——Leetcode天天刷【BFS】(2022.7.31)
CS kill-free pose
Kettle 读取 Excel 数据输出到 Oracle 详解
Use ControlTemplate or Style from resource file in WPF .cs and find the control
net-snmp编译报错:/usr/bin/ld: cannot find crti.o: No such file or directory
单调栈及其应用
Standard C language learning summary 11
不知道这4种缓存模式,敢说懂缓存吗?