当前位置:网站首页>【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;
}
};
边栏推荐
猜你喜欢
随机推荐
Doing Homework HDU - 1074
无代码平台描述文字入门教程
Super Learning Method
北京大学,新迎3位副校长!其中一人为中科院院士!
Small program containers accelerate the construction of an integrated online government service platform
学会使用set和map的基本接口
[代码阅读] CycleGAN: Unpaired Image-To-Image Translation Using Cycle-Consistent Adversarial Networks
Win11文件类型怎么改?Win11修改文件后缀的方法
有12个球,其中11个重量相等,只有1个不一样,不知是轻还是重.用天平秤三次,找出这个球.
Maple 2022 software installation package download and installation tutorial
热成像测温的原理是什么呢?你知道吗?
HCIP 交换实验
使用.NET简单实现一个Redis的高性能克隆版(二)
RL78开发环境
What is the principle of thermal imaging temperature measurement?Do you know?
C language * Xiaobai's adventure
[easyUI]修改datagrid表格中的值
Business collocations
winform 在Datatable插入一笔数据
CompletableFuture接口核心方法介绍









