当前位置:网站首页>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)
}
边栏推荐
- Detailed demonstration pytorch framework implementations old photo repair (GPU)
- Jingdong cloud released a new generation of distributed database StarDB 5.0
- Execute the mysql script file in the docker mysql container and solve the garbled characters
- 标准C语言学习总结11
- 友宏医疗与Actxa签署Pre-M Diabetes TM 战略合作协议
- Detailed AST abstract syntax tree
- Brush the topic of mobile zero power button
- 利用 rpush 和 blpop 实现 Redis 消息队列
- php根据两点经纬度计算距离
- CentOS 7 安装mysql
猜你喜欢

微导纳米IPO过会:年营收4.28亿 君联与高瓴是股东

百利药业IPO过会:扣非后年亏1.5亿 奥博资本是股东

利用 rpush 和 blpop 实现 Redis 消息队列

Introduction to Cosine Distance

涨薪5K必学高并发核心编程,限流原理与实战,分布式计数器限流

【leetcode】剑指 Offer II 008. 和大于等于 target 的最短子数组(滑动窗口,双指针)

JMeter笔记5 |Badboy使用和录制
Teach you to locate online MySQL slow query problem hand by hand, package teaching package meeting

汉源高科8光口12电口交换机千兆8光8电12电16电网管型工业以太网交换机

建模该从哪一步开始?给你分析,给零基础的你一些学习建议
随机推荐
MySQL基础
友宏医疗与Actxa签署Pre-M Diabetes TM 战略合作协议
线上一次JVM FullGC搞得整晚都没睡,彻底崩溃
Introduction to Cosine Distance
The ecological environmental protection management system based on mobile GIS
开源教育论坛| ChinaOSC
DeepMCP网络详解
花 30 美金请 AI 画家弄了个 logo,网友:画得非常好,下次别画了!
ThreadLocal详解
The addition and subtraction of the score of the force deduction brush question (a daily question 7/27)
Line the last time the JVM FullGC make didn't sleep all night, collapse
建模该从哪一步开始?给你分析,给零基础的你一些学习建议
【飞控开发高级教程3】疯壳·开源编队无人机-定高、定点、悬停
高位套牢机构,用友网络的信任危机是如何产生的?
数据驱动的软件智能化开发| ChinaOSC
ECCV2022 | 用于视频问题回答的视频图Transformer
LeetCode 622. Designing Circular Queues
自定义form表单验证
Solidity智能合约开发 — 4.1-合约创建和函数修饰器
ESP8266-Arduino编程实例-MCP4725数模转换器驱动