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 ...
随机推荐
- ubuntu18.04 systemctl
systemd 是 Linux 下的一款系统和服务管理器,兼容 SysV 和 LSB 的启动脚本.systemd 的特性有:支持并行化任务:同一时候採用 socket 式与 D-Bus 总线式激活服务 ...
- CodeWar打怪升级-Python篇
1. The goal of this exercise is to convert a string to a new string where each character in the new ...
- linux上安装phpstudy
摘要:安装:wget-chttp://lamp.phpstudy.net/phpstudy.bin chmod+xphpstudy.bin #权限设置./phpstudy.bin#运行安装用时十到几 ...
- jmeter测试报告套路
各位: 以下是本周第一次双师压测结果,后续优化后会更新优化结果. 配置: 压测环境: staging压测服务器配置4核16G,数据库4核8G 线上环境: 服务器配置16核24G,数据库主库 ...
- ElasticSearch——集群搭建
1.准备 1.1.组件 JDK:1.8版本及以上: ElasticSearch:6.2.4版本: 1.2.服务器 3台服务器 2.安装 2.1.下载解压 wget https://artifacts. ...
- 使用XCode7打包动态库(Framework)
iOS中的静态库和动态库 概念 静态库(Static Library)以 .a 为后缀,它是你的源码的实现.m文件编译而成的二进制文件集合,需要配合上暴漏的.h文件使用,它在引用链接时拷贝至可执行文件 ...
- nginx使用vhost子目录
在主配置文件http模块最后添加如下一句话 [root@host---- ~]# vi /etc/nginx/nginx.conf include /etc/nginx/conf.d/*.conf; ...
- centos下如何关闭xdebug扩展
因为只要加载XDEBUG都会导致系统太慢,所以只有在需要调试系统程序的时候才加载XDEBUG扩展.平时一定要把它给注释掉. 只需要在php.ini文件中注释掉xdebug即可. 即: 前面加一个分号即 ...
- laravel 自带消息notification通知
原文地址:https://blog.csdn.net/zhangxh1013/article/details/53130490
- Tomcat 目录结构
bin目录主要是用来存放tomcat的命令,主要有两大类,一类是以.sh结尾的(linux命令),另一类是以.bat结尾的(windows命令). 很多环境变量的设置都在此处,例如可以设置JDK路径. ...