当前位置:网站首页>[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);
}
}
边栏推荐
- [esp32 learning-2] esp32 address mapping
- Several declarations about pointers [C language]
- I2C bus timing explanation
- 嵌入式启动流程
- Mysqldump error1066 error solution
- ES6语法总结--下篇(进阶篇 ES6~ES11)
- Detailed explanation of Union [C language]
- Classification, understanding and application of common methods of JS array
- ESP learning problem record
- ES6 grammar summary -- Part 2 (advanced part es6~es11)
猜你喜欢

The dolphin scheduler remotely executes shell scripts through the expect command

基於Redis的分布式ID生成器

JS object and event learning notes

Mysql database interview questions

uCOS-III 的特点、任务状态、启动

Custom view puzzle getcolor r.color The color obtained by colorprimary is incorrect

Symbolic representation of functions in deep learning papers

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

JS正则表达式基础知识学习

MySQL時間、時區、自動填充0的問題
随机推荐
【ESP32学习-2】esp32地址映射
vim命令行笔记
(三)R语言的生物信息学入门——Function, data.frame, 简单DNA读取与分析
. elf . map . list . Hex file
Postman 中级使用教程【环境变量、测试脚本、断言、接口文档等】
[golang] leetcode intermediate - fill in the next right node pointer of each node & the k-smallest element in the binary search tree
Problèmes avec MySQL time, fuseau horaire, remplissage automatique 0
JS變量類型以及常用類型轉換
Générateur d'identification distribué basé sur redis
Walk into WPF's drawing Bing Dwen Dwen
荣耀Magic 3Pro 充电架构分析
@The difference between Autowired and @resource
列表的使用
ES6 grammar summary -- Part 2 (advanced part es6~es11)
AMBA、AHB、APB、AXI的理解
C language, log print file name, function name, line number, date and time
MySQL時間、時區、自動填充0的問題
E-commerce data analysis -- salary prediction (linear regression)
ESP8266通过arduino IED连接巴法云(TCP创客云)
程序员老鸟都会搞错的问题 C语言基础 指针和数组