当前位置:网站首页>[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);
}
}
边栏推荐
- E-commerce data analysis -- salary prediction (linear regression)
- C language, log print file name, function name, line number, date and time
- Redis based distributed locks and ultra detailed improvement ideas
- Comparison of solutions of Qualcomm & MTK & Kirin mobile platform USB3.0
- arduino UNO R3的寄存器写法(1)-----引脚电平状态变化
- 1081 rational sum (20 points) points add up to total points
- VSCode基础配置
- MySQL占用内存过大解决方案
- Important methods of array and string
- Cannot change version of project facet Dynamic Web Module to 2.3.
猜你喜欢

Analysis of charging architecture of glory magic 3pro

Comparaison des solutions pour la plate - forme mobile Qualcomm & MTK & Kirin USB 3.0

程序员老鸟都会搞错的问题 C语言基础 指针和数组

Comparison of solutions of Qualcomm & MTK & Kirin mobile platform USB3.0

(一)R语言入门指南——数据分析的第一步
![C language callback function [C language]](/img/7b/910016123738240e24549ddea8a162.png)
C language callback function [C language]

Vscode basic configuration

Understanding of AMBA, AHB, APB and Axi

The dolphin scheduler remotely executes shell scripts through the expect command

E-commerce data analysis -- salary prediction (linear regression)
随机推荐
MySQL時間、時區、自動填充0的問題
@The difference between Autowired and @resource
OSPF message details - LSA overview
ES6 grammar summary -- Part I (basic)
Comparison of solutions of Qualcomm & MTK & Kirin mobile platform USB3.0
荣耀Magic 3Pro 充电架构分析
A possible cause and solution of "stuck" main thread of RT thread
(四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
Générateur d'identification distribué basé sur redis
RuntimeError: cuDNN error: CUDNN_ STATUS_ NOT_ INITIALIZED
【ESP32学习-1】Arduino ESP32开发环境搭建
JS Title: input array, exchange the largest with the first element, exchange the smallest with the last element, and output array.
Basic operations of databases and tables ----- classification of data
Mp3mini playback module Arduino < dfrobotdfplayermini H> function explanation
Reno7 60W超级闪充充电架构
Classification, understanding and application of common methods of JS array
ARM PC=PC+8 最便于理解的阐述
Missing value filling in data analysis (focus on multiple interpolation method, miseforest)
Postman 中级使用教程【环境变量、测试脚本、断言、接口文档等】
Redis 缓存更新策略,缓存穿透、雪崩、击穿问题