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 <= 1000S只由小写字母组成。
思路:
这题的设定其实有点迷,当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 ...
随机推荐
- 微信小程序开发常见坑
前段时间稍微涉猎了微信小程序开发,踩了一些坑,在此总结出来,希望能为小伙伴们提供一点帮助. 页面跳转 对于页面跳转,可能习惯性想到wx.navigateTo,但是在跳转到目标页面是一个tab时,此接口 ...
- expdp / impdp 用法详解 ,和exp / imp 的区别
一 关于expdp和impdp 使用EXPDP和IMPDP时应该注意的事项:EXP和IMP是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用.EXPDP和IMPDP是服务端的工具程 ...
- JAVA安全漫谈1-8笔记
一.反射篇1 classloader就是java的类加载器,告诉虚拟机如何加载这个类.默认情况下根据类名来加载类,类名必须是完整路径 public class class_init { { Syste ...
- 使用Vagrant配置本地开发环境
从二零一四年开始使用vagrant+VirtualBox搭建linux开发环境,配置简单灵活,后台运行占用内存少,比vmware好用很多,果断弃用vmware转投vagrant的怀抱:无论是个人搭建开 ...
- android studio的安装和配置及解决uiautomatorviewer报错
参考博客:https://www.cnblogs.com/singledogpro/p/9551841.html 安装Android Studio 走了不少弯路,现在整理出来,仅当备忘使用. 首先要先 ...
- python 中对象is和==是怎么比较的
Python中的对象包含三要素:id.type.value.其中id用来唯一标识一个对象,type标识对象的类型,value是对象的值.is判断的是a对象是否就是b对象,是通过id来判断的.==判断的 ...
- 完全背包---P1679 神奇的四次方数
P1679 神奇的四次方数 题解 一看这就是个完全背包 m最多不会超过18^4,所以我们把x^4用数组存起来,然后考虑如何填满m,注意存到18^4,不然会像我一样RE... 那么问题就转化成完全背包问 ...
- Activiti核心API
ProcessEngine 说明: 1) 在Activiti中最核心的类,其他的类都是由他而来. 2) 产生方式: 在前面看到了两种创建ProcessEngine(流程引擎)的方式 ...
- Qt编写自定义控件25-自定义QCustomPlot
一.前言 上次在写大屏数据可视化电子看板系统时候,提到过改造QCustomPlot来实现柱状分组图.横向柱状图.横向分组图.鼠标悬停提示等.这次单独列出来描述,有很多人疑问为啥不用QChart,或者e ...
- 《图解 HTTP》读书笔记
<图解 HTTP>一书是日本学者上野宣所著,2014 年由于均良先生翻译并在国内出版.因为作者使用十分生动的语言和浅显易懂的案例将 HTTP 协议讲解得深入浅出,所以深受开发者喜爱.现在在 ...