当前位置:网站首页>每日刷题(day03)——leetcode 899. 有序队列
每日刷题(day03)——leetcode 899. 有序队列
2022-08-10 05:35:00 【introversi0n】
题目名称
- 有序队列
题目内容
给定一个字符串 s 和一个整数 k 。你可以从 s 的前 k 个字母中选择一个,并把它加到字符串的末尾。
返回 在应用上述步骤的任意数量的移动后,字典上最小的字符串 。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/orderly-queue
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
样例
示例 1:
输入:s = “cba”, k = 1
输出:“acb”
解释:
在第一步中,我们将第一个字符(“c”)移动到最后,获得字符串 “bac”。
在第二步中,我们将第一个字符(“b”)移动到最后,获得最终结果 “acb”。
示例 2:
输入:s = “baaca”, k = 3
输出:“aaabc”
解释:
在第一步中,我们将第一个字符(“b”)移动到最后,获得字符串 “aacab”。
在第二步中,我们将第三个字符(“c”)移动到最后,获得最终结果 “aaabc”。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/orderly-queue
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
数据范围
提示:
1 <= k <= S.length <= 1000
s 只由小写字母组成。
思路
当k>=2时,字符串就可以在有限次的移动后得到所有的重排,因此只需要对字符串进行sort排序最后输出
如果k=1,那么就找出s.length种排列情况,找到最小的string返回即可
作者:introversi0n
链接:https://leetcode.cn/problems/orderly-queue/solution/c-by-introversi0n-che7/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
代码
class Solution {
public:
string orderlyQueue(string s, int k) {
string minstr = s;
if(k != 1){
sort(s.begin(),s.end());
return s;
}
for (int i=0;i<s.length();i++)
{
string temp;
temp=s.substr(i,s.length()-i)+s.substr(0,i);
minstr = min(minstr,temp);
}
return minstr;
}
};
边栏推荐
猜你喜欢
随机推荐
深度学习阶段性报告(一)
LeetCode 94.二叉树的中序遍历(简单)
序列化、编码、requests库json和data参数
LeetCode 94. Inorder Traversal of Binary Trees (Simple)
51单片机ST188手持人体温度脉搏心率测量仪锂电池充电
程序员副业赚钱之道,实现月收入增加20K
LeetCode 剑指offer 21.调整数组顺序使奇数位于偶数前面(简单)
常用模块封装-pymysql、pymongo(可优化)
一个基于.Net Core 开源的物联网基础平台
51单片机室内环境甲醛PM2.5光照温度湿度检测及窗帘加湿消毒控制系统
.NET操作Excel高效低内存的开源框架 - MiniExcel
并查集原理与API设计
The way for programmers to make money from a sideline business and increase their monthly income by 20K
Machine Learning - Clustering - Shopping Mall Customer Clustering
Notes for RNN and Decision Tree
离散数学的学习记录
Convolutional Neural Network (CNN) for Clothing Image Classification
力扣——统计只差一个字符的子串数目
String common methods
Explain the principle of MySql index in detail