LeetCode 899. Orderly Queue
899. Orderly Queue(有序队列)
题目:
给出了一个由小写字母组成的字符串 S
。然后,我们可以进行任意次数的移动。
在每次移动中,我们选择前 K
个字母中的一个(从左侧开始),将其从原位置移除,并放置在字符串的末尾。
返回我们在任意次数的移动之后可以拥有的按字典顺序排列的最小字符串。
示例 1:
输入:S = "cba", K = 1
输出:"acb"
解释:
在第一步中,我们将第一个字符(“c”)移动到最后,获得字符串 “bac”。
在第二步中,我们将第一个字符(“b”)移动到最后,获得最终结果 “acb”。
示例 2:
输入:S = "baaca", K = 3
输出:"aaabc"
解释:
在第一步中,我们将第一个字符(“b”)移动到最后,获得字符串 “aacab”。
在第二步中,我们将第三个字符(“c”)移动到最后,获得最终结果 “aaabc”。
提示:
1 <= K <= S.length <= 1000
S
只由小写字母组成。
思路:
这题的设定其实有点迷,当K==1时,就代表前后次序(相对位置)并没有改变,只是在开头的可以移到后端。当K!=1时,就代表可以随意组合,直接计算字典最小的序列即可。
直接分类讨论,=1时,新建S=S+S,从前往后取len位比较即可;!=1时,拆为数组,排序,组合即可。
代码:
public static String orderlyQueue(String S, int K)
{
int len = S.length(); if(K==1)
{
String word = S;
S = S + S;
for(int i = 0;i < len;i++)
{
if(word.compareTo(S.substring(i,i+len))>0)
word = S.substring(i,i+len);
}
return word;
}
else
{
char [] word = S.toCharArray();
Arrays.sort(word);
StringBuilder sb=new StringBuilder();
for(int i=0;i<S.length();i++)
sb.append(word[i]);
return sb.toString();
}
}
LeetCode 899. Orderly Queue的更多相关文章
- [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- ...
- 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] Design Circular Queue 设计环形队列
Design your implementation of the circular queue. The circular queue is a linear data structure in w ...
- [LeetCode] 232. Implement Queue using Stacks 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- 【LeetCode】406. Queue Reconstruction by Height 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- Java for LeetCode 232 Implement Queue using Stacks
Stack<Integer> stack=new Stack<Integer>(); public void push(int x) { stack.push(x); } // ...
- Leetcode 232 Implement Queue using Stacks STL
本题用两个栈实现队列,用栈的基本操作去实现队列的所有基本操作push(),pop(),peek()以及empty() sa作为输入栈,sb作为输出栈,将sa输入元素的反转过来放到sb中 push与sa ...
- (easy)LeetCode 232.Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
随机推荐
- Nginx内置变量及正则语法
对于很多Nginx初学者来说,配置文件是必须要看懂的.但是当公司的Nginx配置文件放在你面前的时候你总会被一些带着"$"符号和一大推看不懂的的正则给正懵逼.没错带着"$ ...
- maven打包遇到错误,Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.1:test
对Pom文件进行配置(亲自尝试,已成功解决) <build> <plugins> <plugin> <groupId>org.apache.maven. ...
- thinkphp5 更改入口文件在主目录
默认thinkPHP入口文件在public/index.php,而在虚拟主机部署时,不能设置访问路径,因此需要将入口文件放置在主目录上. 一.主目录下新建index.php 复制以下内容 // 定义应 ...
- Spring事务管理2----编程式事务管理
编程式事务管理 通过使用将Spring框架提供的TransactionTemplate模板注入到业务层来进行事务管理,这样对业务层原来的代码修改过多.不利于项目的后期维护. 以下是声明式事务管理的具体 ...
- 工具的更新换代 总是要折腾一下Windows10下Java Web 开发环境的配置
Windows10下Java Web 开发环境的配置 由于经常性遗忘,所以整理一下 Java Web 开发环境的搭建与配置,利人利己 主要分为以下几步,需要可以挑着看 Windows下 JDK 的下载 ...
- [C++]哈夫曼树(最优满二叉树) / 哈夫曼编码(贪心算法)
一 哈夫曼树 1.1 基本概念 算法思想 贪心算法(以局部最优,谋求全局最优) 适用范围 1 [(约束)可行]:它必须满足问题的约束 2 [局部最优]它是当前步骤中所有可行选择中最佳的局部选择 3 [ ...
- 不要在viewWillDisappear:方法中移除通知
都知道viewWillAppear:方法是在控制器的view将要显示的时候调用的,而viewWillDisappear:方法是在控制器的view将要隐藏的时候调用. 这看上去没什么问题,键盘的显示和隐 ...
- System.Web.UI.Page的用法,一定要学会懒
在ASP.NET中,任何页面都是继承于System.Web.UI.Page,他提供了ASP.NET中的Response,Request,Session,Application的操作.在使用Visual ...
- python函数,定义,参数,返回值
python中可以将某些具备一定功能的代码写成一个函数,通过函数可以在一定程度上减少代码的冗余,节约书写代码的时间.因为有一些代码实现的功能我们可能会在很多地方用到. 1.函数的声明与定义 通过def ...
- IDEA配置虚拟机内存
修改idea64.exe.vmoptions(64位电脑选择此文件) 一个例子,电脑内存8G,设置如下: -Xms1024m -Xmx4096m -XX:MaxPermSize=1024m -XX:R ...