当前位置:网站首页>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;}};边栏推荐
- 安装anaconda并创建虚拟环境
- 模板字符串概述
- In-depth understanding of JVM-memory structure
- 【HiFlow】经常忘记签到怎么办?使用腾讯云场景连接器每天提醒你。
- 头条服务端一面经典10道面试题解析
- ESP8266-Arduino编程实例-MCP4725数模转换器驱动
- 数学之美 第六章——信息的度量和作用
- Use ControlTemplate or Style from resource file in WPF .cs and find the control
- LeetCode 622. 设计循环队列
- 1161 最大层内元素和——Leetcode天天刷【BFS】(2022.7.31)
猜你喜欢
随机推荐
小马智行起诉擎天智卡:索赔6000万 彭军称要斗争到底
危化企业双重预防机制数字化建设进入全面实施阶段
RNA核糖核酸修饰荧光染料|HiLyte Fluor 488/555/594/647/680/750标记RNA核糖核酸
信使mRNA甲基化偶联3-甲基胞嘧啶(m3C)|mRNA-m3C
Leetcode sword refers to Offer 15. 1 in the binary number
力扣206-反转链表——链表
NNLM、RNNLM等语言模型 实现 下一单词预测(next-word prediction)
从腾讯阿里等大厂出来创业搞 Web3、元宇宙的人在搞什么
调用EasyCVR接口时视频流请求出现404,并报错SSL Error,是什么原因?
Node version switching tool NVM and npm source manager nrm
php截取中文字符串实例
glusterfs 搭建使用
第三方验收测试报告有什么作用?如何获取权威软件测试报告?
自定义form表单验证
Matlab paper illustration drawing template No. 42 - bubble matrix diagram (correlation coefficient matrix diagram)
李沐动手学深度学习V2-BERT微调和代码实现
ES6-箭头函数
ES6--剩余参数
tRNA-m5C转运RNA(tRNA)修饰5-甲基胞嘧啶(m5C)|tRNA修饰m1Am2A (2-methyladenosine)
抖音web逆向教程









![【微信小程序2】事件传参与数据同步[03]](/img/d9/73004e6edf800c583231a94dfbd878.png)