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. centos7 设置 防火墙 开机自启

    CentOS 7.0默认使用的是firewall作为防火墙,之前版本是使用iptables. 1.设置firewall开机启动 systemctl enable firewalld 2.禁止firew ...

  2. [MySql]当虚拟机的IP地址自动更换后,JDBC使用原来的配置连不上MySql数据库时所报的异常。

    Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. ...

  3. WebView调用js方法获取返回值的完美解决方案

    在Android项目中我们或多或少会涉及到与js交互的问题,这其中WebView是必须掌握的控件,今天主要说说我们通过WebView调用js方法,然后如何很好的获取返回值.这里我总结了三种方式,大家可 ...

  4. Jetson TK下如何写汇编语言

    首先,可以根据http://www.cnblogs.com/zenny-chen/p/3816620.html来安装CUDA工具链.这个工具集里包含了CUDA编译器以及其它必要的工具.然后,我们进入/ ...

  5. 15 Flutter BottomNavigationBar自定义底部导航条 以及实现页面切换 以及模块化

    效果: /**  * Flutter  BottomNavigationBar 自定义底部导航条.以及实现页面切换:  * BottomNavigationBar是底部导航条,可以让我们定义底部Tab ...

  6. Python生成随机数组的方法小结

    Python生成随机数组的方法小结 本文实例讲述了Python生成随机数组的方法.分享给大家供大家参考,具体如下: 研究排序问题的时候常常需要生成随机数组来验证自己排序算法的正确性和性能,今天把Pyt ...

  7. pycharm连接linux版python

    1.建立连接 2.测试连接 3.同步目录 4.查看同步的目录 5.设置永久同步目录 6.设置连接 可以看到添加进来了 参照文档: https://www.cnblogs.com/xiao-apple3 ...

  8. jenkins容器内修改root密码--ubuntu系统

    http://www.voidcn.com/article/p-yvnoogkc-ng.html 由于jenkins官方镜像是ubuntu系统,所有啥的都用 sudo 换到root账号,然后登陆har ...

  9. centOS7下的静态Ip的配置。

    centOS7下NAT的静态网卡的配置 最近在cenOS7下搭建大数据,发现centOS7配置静态ip的必要性.这篇博文就来谈谈如何VM虚拟机中配置centOS7的静态ip.如有不足,还望志同道合者纠 ...

  10. Java进程监控

    目录 1.引言 2. 程序启停, 为进程自定义项目名称 3. 操作系统判断 4. 获取进程信息 5. 内存,CPU信息 6. 堆内存信息 7. 端口信息 8. 线程信息 9. MXBean使用样例 9 ...