当前位置:网站首页>[899]有序队列
[899]有序队列
2022-07-06 09:17:00 【劲腰傩舞】
题目概要
给定一个字符串。在规则限定内,把其调整我字典序最小的情况。
题目
给定一个字符串 s 和一个整数 k 。你可以从 s 的前 k 个字母中选择一个,并把它加到字符串的末尾。
返回 在应用上述步骤的任意数量的移动后,字典上最小的字符串 。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/orderly-queue
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
示例1
输入:s = “cba”, k = 1
输出:“acb”
解释:
在第一步中,我们将第一个字符(“c”)移动到最后,获得字符串 “bac”。
在第二步中,我们将第一个字符(“b”)移动到最后,获得最终结果 “acb”。
示例2
输入:s = “baaca”, k = 3
输出:“aaabc”
解释:
在第一步中,我们将第一个字符(“b”)移动到最后,获得字符串 “aacab”。
在第二步中,我们将第三个字符(“c”)移动到最后,获得最终结果 “aaabc”。
public String orderlyQueue(String s, int k) {
/*只能移动一个字符的情况下。可以把字符串当成循环链表处理*/
if(k==1){
String ans=s;
/*那么需要判断以任意一个位置作为起点的字符串的字典序*/
for (int i = 0; i < s.length(); i++) {
String temp=s.substring(i)+s.substring(0,i);
if(temp.compareTo(ans)<0)
ans=temp;
}
return ans;
}
/*k>=2的时候。可以证:可以任意调换给定字符串的两个字符。即最终字符串的字母序可调到最低*/
else{
/*转换成字符数组之后就可以排序了*/
char[] ca = s.toCharArray();
/*sort函数默认升序*/
Arrays.sort(ca);
/*不要尝试对字符数组toString:返回的是类型和对应的哈希码。貌似除了string的tostring是被重载了的。其他都是直接继承object*/
return new String(ca);
}
}
边栏推荐
- MySQL time, time zone, auto fill 0
- Pat 1097 duplication on a linked list (25 points)
- arduino UNO R3的寄存器写法(1)-----引脚电平状态变化
- 关于Gateway中使用@Controller的问题
- Symbolic representation of functions in deep learning papers
- 基於Redis的分布式ID生成器
- map文件粗略分析
- ES6语法总结--上篇(基础篇)
- Classification, understanding and application of common methods of JS array
- Esp8266 uses Arduino to connect Alibaba cloud Internet of things
猜你喜欢
OPPO VOOC快充电路和协议
JS Title: input array, exchange the largest with the first element, exchange the smallest with the last element, and output array.
Missing value filling in data analysis (focus on multiple interpolation method, miseforest)
(四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
基於Redis的分布式ID生成器
JS variable types and common type conversions
JS数组常用方法的分类、理解和运用
(一)R语言入门指南——数据分析的第一步
ESP8266使用arduino连接阿里云物联网
NRF24L01故障排查
随机推荐
open-mmlab labelImg mmdetection
Unit test - unittest framework
MySQL时间、时区、自动填充0的问题
vim命令行笔记
Kaggle competition two Sigma connect: rental listing inquiries
Arduino gets the length of the array
ESP8266使用arduino连接阿里云物联网
ARM PC=PC+8 最便于理解的阐述
Reno7 60W super flash charging architecture
Redis based distributed locks and ultra detailed improvement ideas
JS变量类型以及常用类型转换
如何给Arduino项目添加音乐播放功能
Basic operations of databases and tables ----- classification of data
Reno7 60W超级闪充充电架构
Kconfig Kbuild
ES6 grammar summary -- Part 2 (advanced part es6~es11)
Esp8266 connects to onenet cloud platform (mqtt) through Arduino IDE
Comparaison des solutions pour la plate - forme mobile Qualcomm & MTK & Kirin USB 3.0
js 变量作用域和函数的学习笔记
VIM command line notes