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.   1 <= K <= S.length <= 1000
    2.   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的更多相关文章

  1. [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 ...

  2. 【LeetCode】899. Orderly Queue 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/orderly- ...

  3. 899. Orderly Queue

    A string S of lowercase letters is given.  Then, we may make any number of moves. In each move, we c ...

  4. [LeetCode] Design Circular Queue 设计环形队列

    Design your implementation of the circular queue. The circular queue is a linear data structure in w ...

  5. [LeetCode] 232. Implement Queue using Stacks 用栈来实现队列

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  6. 【LeetCode】406. Queue Reconstruction by Height 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. Java for LeetCode 232 Implement Queue using Stacks

    Stack<Integer> stack=new Stack<Integer>(); public void push(int x) { stack.push(x); } // ...

  8. Leetcode 232 Implement Queue using Stacks STL

    本题用两个栈实现队列,用栈的基本操作去实现队列的所有基本操作push(),pop(),peek()以及empty() sa作为输入栈,sb作为输出栈,将sa输入元素的反转过来放到sb中 push与sa ...

  9. (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 ...

随机推荐

  1. Linux设备驱动程序 之 vmalloc

    vmalloc()函数的工作方式类似于kmalloc(),只不过在前者分配的内存虚拟地址是连续的,而物理地址则无须连续:这也是用户空间分配函数的工作方式:由malloc()返回的页在进程的虚拟地址空间 ...

  2. MySQL 5.7新增加的json数据类型

    MySQL 5.7中有json存储类型了以前我们只能通过php来进行序列化了不过现在就不需要了我们可以直接使用MySQL 5.7的json数据类型来存储json格式数据了,具体来看介绍.   在MyS ...

  3. Projects: Linux scalability: Accept() scalability on Linux 惊群效应

    小结: 1.不必要的唤醒 惊群效应 https://github.com/benoitc/gunicorn/issues/792#issuecomment-46718939 https://www.c ...

  4. ColorDrawable

    最简单的一种Drawable,当我们将ColorDrawable绘制到Canvas(画布)上的时候, 会使用一种固定的颜色来填充Paint,然后在画布上绘制出一片单色区域! 1).Java中定义Col ...

  5. kotlin之lambda表达式和匿名函数

    lambda表达式,称为匿名函数,是一种函数字面值,也就是没有声明的函数,但可以作为表达式传递出去. 函数类型: 对于接受另一个函数的作为自己的参数,必须针对这个参数指定一个函数的类型如 fun &l ...

  6. Django - ORM - 事务, 乐观锁, 悲观锁

    事务 概念 Transaction 事务:一个最小的不可再分的工作单元:通常一个事务对应一个完整的业务(例如银行账户转账业务,该业务就是一个最小的工作单元) 一个完整的业务需要批量的DML(inser ...

  7. js图片转base64编码

    let reader = new FileReader(); reader.readAsDataURL(file.raw); reader.onload = () => { let imageU ...

  8. 分布式存储——Build up a High Availability Distributed Key-Value Store

    原文链接 Preface There are many awesome and powerful distributed NoSQL in the world, like Couchbase, Mon ...

  9. app怎么获取package与active name

    1.aapt dump badging apk名称 2.adb logcat | grep START 或者 adb shell "logcat | grep START" 然后在 ...

  10. MVP模式的学习

    简书: http://www.jianshu.com/p/f6fd9656f619 Github: https://github.com/tsaievan/YFMVPDemo