当前位置:网站首页>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
消除对特权账户的依赖使用Kaniko构建镜像
云服务器如何安全使用本地的AD/LDAP?
深入理解JVM-内存结构
JS 内置构造函数 扩展 prototype 继承 借用构造函数 组合式 原型式creat 寄生式 寄生组合式 call apply instanceof
算法--交错字符串(Kotlin)
NNLM、RNNLM等语言模型 实现 下一单词预测(next-word prediction)
2022 年值得尝试的 7 个 MQTT 客户端工具
YARN功能介绍、交互流程及调度策略
倒计时2天,“文化数字化战略新型基础设施暨文化艺术链生态建设发布会”启幕在即
随机推荐
charles配置客户端请求全部不走缓存
leetcode 072. Finding Square Roots
盲埋孔PCB叠孔设计的利与弊
利用 rpush 和 blpop 实现 Redis 消息队列
从腾讯阿里等大厂出来创业搞 Web3、元宇宙的人在搞什么
Matlab paper illustration drawing template No. 42 - bubble matrix diagram (correlation coefficient matrix diagram)
2022.8.2
WPF .cs中使用资源文件中的ControlTemplate或Style并找到控件
ES6--剩余参数
ES6解构赋值--数组解构及对象解构
机器学习中专业术语的个人理解与总结(纯小白)
tensorflow-gpu2.4.1安装配置详细步骤
MapReduce介绍及执行过程
力扣206-反转链表——链表
leetcode 072. 求平方根
leetcode 268. 丢失的数字(异或!!)
揭秘5名运维如何轻松管理数亿级流量系统
leetcode 136. 只出现一次的数字(异或!!)
数学之美 第六章——信息的度量和作用
Auto.js实现朋友圈自动点赞