当前位置:网站首页>[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);
}
}
边栏推荐
- Pytorch: tensor operation (I) contiguous
- Missing value filling in data analysis (focus on multiple interpolation method, miseforest)
- Types de variables JS et transformations de type communes
- ESP学习问题记录
- Kconfig Kbuild
- 关于Gateway中使用@Controller的问题
- JS function promotion and declaration promotion of VaR variable
- 如何给Arduino项目添加音乐播放功能
- Working principle of genius telephone watch Z3
- open-mmlab labelImg mmdetection
猜你喜欢
随机推荐
Important methods of array and string
【ESP32学习-1】Arduino ESP32开发环境搭建
Postman 中级使用教程【环境变量、测试脚本、断言、接口文档等】
AMBA、AHB、APB、AXI的理解
STM32 how to locate the code segment that causes hard fault
如何给Arduino项目添加音乐播放功能
Keyword inline (inline function) usage analysis [C language]
Arm pc=pc+8 is the most understandable explanation
Custom view puzzle getcolor r.color The color obtained by colorprimary is incorrect
JS数组常用方法的分类、理解和运用
Detailed explanation of 5g working principle (explanation & illustration)
Flink late data processing (3)
Knowledge summary of request
ESP8266通过Arduino IDE连接Onenet云平台(MQTT)
level16
ARM PC=PC+8 最便于理解的阐述
Cannot change version of project facet Dynamic Web Module to 2.3.
高通&MTK&麒麟 手机平台USB3.0方案对比
Common DOS commands
MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解









