899. Orderly Queue
A string
Sof lowercase letters is given. Then, we may make any number of moves.In each move, we choose one of the first
Kletters (starting from the left), remove it, and place it at the end of the string.Return the lexicographically smallest string we could have after any number of moves.
Example 1:
Input: S = "cba", K = 1
Output: "acb"
Explanation:
In the first move, we move the 1st character ("c") to the end, obtaining the string "bac".
In the second move, we move the 1st character ("b") to the end, obtaining the final result "acb".Example 2:
Input: S = "baaca", K = 3
Output: "aaabc"
Explanation:
In the first move, we move the 1st character ("b") to the end, obtaining the string "aacab".
In the second move, we move the 3rd character ("c") to the end, obtaining the final result "aaabc".
Note:
1 <= K <= S.length <= 1000Sconsists of lowercase letters only.
Approach #1: String. [Java]
class Solution {
public String orderlyQueue(String S, int K) {
if (K >= 2) {
char[] arr = S.toCharArray();
Arrays.sort(arr);
return new String(arr);
}
String ret = S;
int len = S.length();
for (int i = 0; i < len; ++i) {
String temp = S.substring(1) + S.charAt(0);
if (temp.compareTo(ret) < 0)
ret = temp;
S = temp;
}
return ret;
}
}
Analysis:
1. When K == 1:
We can only rotate the whole string. There are S.length different states and we return the lexicographically smallest string.
2. When K >= 2 you can swap any 2 character in the string:
Assume u have "abcdefg", put "b" into tail, and ten paut "acdefg" into the tail. Then you have "bacdefg". Based on this, u can swap first a and b.
Because the string is a ring, u can sliding it. This means you can swap any 2 character in the string. e.g. "abcdefg" -> "acdefgb" -> "cadefgb".
Reference:
https://leetcode.com/problems/orderly-queue/discuss/165857/Java-Simple-Solution-12-ms
899. Orderly Queue的更多相关文章
- LeetCode 899. Orderly Queue
899. Orderly Queue(有序队列) 题目: 给出了一个由小写字母组成的字符串 S.然后,我们可以进行任意次数的移动. 在每次移动中,我们选择前 K 个字母中的一个(从左侧开始),将其从原 ...
- [LeetCode] 899. Orderly Queue 有序队列
A string S of lowercase letters is given. Then, we may make any number of moves. In each move, we c ...
- 【LeetCode】899. Orderly Queue 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/orderly- ...
- [Swift]LeetCode899. 有序队列 | Orderly Queue
A string S of lowercase letters is given. Then, we may make any number of moves. In each move, we c ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- leetcode hard
# Title Solution Acceptance Difficulty Frequency 4 Median of Two Sorted Arrays 27.2% Hard ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- 【LeetCode】数学(共106题)
[2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
随机推荐
- linux命令的笔记
1.改变目录的用户组和所有者 chown 命令 如下图: 可以看到test1与test2的的所有者和所属组都是root,其中 第三个字段是说明目录拥有者, 第四个字段是文件拥有者所在的组, 第五个字段 ...
- 编写高质量代码改善C#程序的157个建议——建议151:使用事件访问器替换公开的事件成员变量
建议151:使用事件访问器替换公开的事件成员变量 事件访问器包含两部分内容:添加访问器和删除访问器.如果涉及公开的事件字段,应该始终使用事件访问器.代码如下所示: class SampleClass ...
- java 访问HTTPS rest服务
import java.io.*;import java.net.*;import java.security.cert.CertificateException;import java.securi ...
- Python 操作 Excel 、txt等文件
#xlrd 读取excel import xlrd import os #获取文件路径 filepath = os.path.join(os.getcwd(),'user_info') #获取文件名称 ...
- DLL用def定义文件来导出重载函数(转)
动态链接库DLL_Sample.dll DLL_Sample.h:#ifdef TEST_API# define TEST_API _declspec(dllexport)#else# define ...
- Maven整理笔记の安装及配置
第一部分:在Windows上安装Maven 检查JDK的安装 在安装Maven之前,首先确认你已经正确安装了JDK.Maven可以运行在JDK1.4及以上版本.先打开Windows命令,运行 ...
- Apache Shiro去掉URL中的JSESSIONID
如果你的shiro版本在1.3.2版本以上这个BUG已经解决只需要在配置文件如下配置中添加红色部分 <!-- 会话管理器 --> <bean id="sessionMana ...
- [LeetCode 题解]: Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- Windows上传文件到linux 使用winscp
Windows上传文件到linux 使用winscp, winscp下载目录 https://sourceforge.net/projects/winscp/postdownload?source=d ...
- zookeeper 开机启动
第一种:直接修改/etc/rc.d/rc.local文件 在/etc/rc.d/rc.local文件中需要输入两行,其中export JAVA_HOME=/usr/java/jdk1.8.0_112是 ...