当前位置:网站首页>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;}};边栏推荐
猜你喜欢
[email protected] 594/[email prote"/>RNA核糖核酸修饰Alexa 568/[email protected] 594/[email prote

622 设计循环队列——Leetcode天天刷【循环队列,数组模拟,双指针】(2022.8.2)

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

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

消除对特权账户的依赖使用Kaniko构建镜像

子树的大小

RNA核糖核酸修饰RNA-HiLyte FluorTM 405荧光染料|RNA-HiLyte FluorTM 405

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

CLIP论文解读

Pytorch GPU 训练环境搭建
随机推荐
Matlab paper illustration drawing template No. 42 - bubble matrix diagram (correlation coefficient matrix diagram)
剑指 Offer II 044. 二叉树每层的最大值-dfs法
Golang死信队列的使用
ESP8266-Arduino编程实例-WS2812驱动
matplotlib画polygon, circle
【飞控开发高级教程6】疯壳·开源编队无人机-AI语音控制
ThreadLocal详解
第三方验收测试报告有什么作用?如何获取权威软件测试报告?
子结点的数量(2)
倒计时2天,“文化数字化战略新型基础设施暨文化艺术链生态建设发布会”启幕在即
高并发,你真的理解透彻了吗?
EMQX Newsletter 2022-07|EMQX 5.0 正式发布、EMQX Cloud 新增 2 个数据库集成
node版本切换工具NVM以及npm源管理器nrm
【HiFlow】经常忘记签到怎么办?使用腾讯云场景连接器每天提醒你。
演讲议题及嘉宾重磅揭晓,TDengine 开发者大会推动数据技术“破局”
8.3模拟赛总结
8.2模拟赛总结
In-depth understanding of JVM-memory structure
PHP according to the longitude and latitude calculated distance two points
Why BI software can't handle correlation analysis