当前位置:网站首页>每日刷题(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;
}
};
边栏推荐
- 【目标检测】相关指标的引入与解析
- STM32单片机手机APP蓝牙高亮RGB彩灯控制板任意颜色亮度调光
- Gradle学习 (一) 入门
- pytorch-11. Convolutional Neural Network (Advanced)
- String common methods
- pytorch-10. Convolutional Neural Networks (homework)
- Tensorflow 2.0 使用流程详解
- Exploratory Data Analysis EDA
- 微信小程序--模板与设置WXML
- [List Exercise] Traverse the collection and sort by price from low to high,
猜你喜欢

自定义View的流程总结学习

Explain the principle of MySql index in detail

pytorch-10. Convolutional Neural Networks (homework)

分享一款恋爱星座男女配对微信小程序源码

pytorch-06.逻辑斯蒂回归

Notes for RNN and Decision Tree

以STM32F103C6TA为例通过配置CubeMX实现GPIO输出完成点灯实例

.Net Core imports tens of millions of data to Mysql

LeetCode 2011.执行操作后的变量值(简单)

Day1 微信小程序-小程序代码的构成
随机推荐
Decentralized and p2p networks and traditional communications with centralization at the core
LeetCode 1351.统计有序矩阵中的负数(简单)
解决错误 Could not find method leftShift() for arguments
LeetCode 1894. Find the student number that needs to be supplemented with chalk
pytorch-06. Logistic regression
pytorch-10.卷积神经网络(作业)
LeetCode 94. Inorder Traversal of Binary Trees (Simple)
STM32F407ZG PWM
一个基于.Net Core 开源的物联网基础平台
LeetCode 2011. Variable Value After Action (Simple)
卷积神经网络(CNN)实现服装图像分类
Collection set interface
【简易笔记】PyTorch官方教程简易笔记 EP2
深度学习TensorFlow入门环境配置
(Flutter报错)Cannot run with sound null safety, because the following dependencies
LeetCode 1720.解码异或后的数组(简单)
PyTorch之训练技巧
STM32单片机OLED经典2048游戏单片机小游戏
Exploratory Data Analysis EDA
Radon 变换原理和应用
