当前位置:网站首页>Leetcode 899. An orderly queue
Leetcode 899. An orderly queue
2022-08-03 20:12:00 【Luna who can program】
Given a string s and an integer k .You can choose one of the first k letters of s and add it to the end of the string.
returns the lexicographically smallest string after applying any number of moves from the above steps.
Example 1:
Input: s = "cba", k = 1
Output: "acb"
Explanation:
In the first step, we move the first character ("c") to the end, to get the string "bac".
In the second step, we move the first character ("b") to the end to get the final result "acb".
Example 2:
Input: s = "baaca", k = 3
Output: "aaabc"
Explanation:
In the first step, we move the first character ("b") to the end, to get the string "aacab".
In the second step, we move the third character ("c") to the end to get the final result "aaabc".
Tip:
1 <= k <= S.length <= 1000
s consists of lowercase letters only.
Thinking:
When k==1, the original string is regarded as a ring, the first character is placed at the end every time from the beginning to the end, and the lexicographical order among these characters is saved and simulated directly.
When k>=2, after a limited number of operations, it must be in ascending order, because we can choose the largest one in the first k every time and put it at the end, which is very troublesome in practice, but withThe result of sorting is the same, so you can return directly to sort.
class Solution {public:string orderlyQueue(string s, int k) {if(k>1){sort(s.begin(),s.end());return s;}string ans=s;for(int i=0;i<s.size();++i){s=string{s.begin()+1,s.end()}+s[0];span>ans=min(ans,s);}return ans;}};边栏推荐
猜你喜欢

Alexa染料标记RNA核糖核酸|RNA-Alexa 514|RNA-Alexa 488|RNA-Alexa 430

不知道这4种缓存模式,敢说懂缓存吗?

亚马逊云科技 Build On 2022 - AIot 第二季物联网专场实验心得

RNA核糖核酸修饰荧光染料|HiLyte Fluor 488/555/594/647/680/750标记RNA核糖核酸

详解AST抽象语法树

算法--交错字符串(Kotlin)

一种能有效缓解环境噪声对音频质量干扰的方案

Why BI software can't handle correlation analysis

面试官:为什么 0.1 + 0.2 == 0.300000004?

abs()、fabs() 和 labs() 的区别
随机推荐
深入理解JVM-内存结构
Anaconda 虚拟环境迁移
WPF .cs中使用资源文件中的ControlTemplate或Style并找到控件
1161 最大层内元素和——Leetcode天天刷【BFS】(2022.7.31)
2022 年值得尝试的 7 个 MQTT 客户端工具
leetcode 136. 只出现一次的数字(异或!!)
揭秘5名运维如何轻松管理数亿级流量系统
汉源高科8光口12电口交换机千兆8光8电12电16电网管型工业以太网交换机
抖音web逆向教程
leetcode 072. 求平方根
盲埋孔PCB叠孔设计的利与弊
收藏-即时通讯(IM)开源项目OpenIM-功能手册
C51 存储类型与存储模式
ES6--剩余参数
消除对特权账户的依赖使用Kaniko构建镜像
LeetCode 1374. 生成每种字符都是奇数个的字符串
力扣206-反转链表——链表
机器学习中专业术语的个人理解与总结(纯小白)
NNLM、RNNLM等语言模型 实现 下一单词预测(next-word prediction)
软件测试基本流程有哪些?权威的第三方软件检测机构推荐