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 Basic学习笔记01
介绍课程: 中级: 初级:系统基础 中级:系统管理.服务安全及服务管理.Shell脚本: 高级: MySQL数据库: cache & storage 集群: Cluster lb: 4laye ...
- Ubuntu14.04 下安装Samba服务
1.更改linux镜像源: # vim /etc/apt/sources.list deb http://mirrors.163.com/ubuntu/ trusty main restricted ...
- 解决Emoji存储MySQL报错问题
在解决之前,得先说明一下为什么会出现报错,Emoji表情占用4个字节,但是MySQL数据库UTF-8编码最多只能存储3个字节,就会导致存储不进去 如何解决Emoji存储问题 mysql 的 utf8编 ...
- 浅说Java反射机制
工作中遇到,问题解决: JAVA语言中的反射机制: 在Java 运行时 环境中,对于任意一个类,能否知道这个类有哪些属性和方法? 对于任意一个对象,能否调用他的方法?这些答案是肯定的,这种动态获取类的 ...
- Generated by NetworkManager、ubuntu DNS设置丢失(network-manager造成的情况)
方法一:去掉重启 方法二:卸载network-manager 实测网络不稳,经常掉线(kalinux2.0环境)
- java.awt.Graphics2D 图片缩放
关键字:java image thumbnail google 粗略demo: import java.awt.Graphics2D; import java.awt.GraphicsConfig ...
- Unity开发小技巧整理
- CodeForces 227E Anniversary (斐波那契的高妙性质+矩阵快速幂)
There are less than 60 years left till the 900-th birthday anniversary of a famous Italian mathemati ...
- Verilog MIPS32 CPU(二)-- Regfiles
Verilog MIPS32 CPU(一)-- PC寄存器 Verilog MIPS32 CPU(二)-- Regfiles Verilog MIPS32 CPU(三)-- ALU Verilog M ...
- 一个简单的C#+arcgis的非数据库版例子
1.首先新建一个winform的项目. 2.确保C#工具箱包含ESRI的相关控件,如果没有就需要安装SDK. 如果VS中依旧不存在esri控件解决方案如下,以VS2013为例: (1)打开注册表,定位 ...