当前位置:网站首页>[899] ordered queue
[899] ordered queue
2022-07-06 12:25:00 【Vigorous waist Nuo dance】
Topic summary
Given a string . Within the limits of the rules , Adjust it to the minimum order in my dictionary .
subject
Given a string s And an integer k . You can start your s Before k Select one of the letters , And add it to the end of the string .
return After applying any number of movements of the above steps , The smallest string in the dictionary .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/orderly-queue
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Example 1
Input :s = “cba”, k = 1
Output :“acb”
explain :
In the first step , We'll take the first character (“c”) Move to the end , Get string “bac”.
In the second step , We'll take the first character (“b”) Move to the end , Get the final result “acb”.
Example 2
Input :s = “baaca”, k = 3
Output :“aaabc”
explain :
In the first step , We'll take the first character (“b”) Move to the end , Get string “aacab”.
In the second step , We'll take the third character (“c”) Move to the end , Get the final result “aaabc”.
public String orderlyQueue(String s, int k) {
/* When only one character can be moved . You can treat the string as a circular linked list */
if(k==1){
String ans=s;
/* Then you need to determine the dictionary order of the string starting from any position */
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 When . It can be proved : You can exchange two characters of a given string at will . That is, the alphabetical order of the final string can be adjusted to the lowest */
else{
/* After being converted into a character array, you can sort */
char[] ca = s.toCharArray();
/*sort Functions are in ascending order by default */
Arrays.sort(ca);
/* Don't try to match character arrays toString: What is returned is the type and the corresponding hash code . It seems that except for string Of tostring It's overloaded . Others are inherited directly object*/
return new String(ca);
}
}
边栏推荐
- open-mmlab labelImg mmdetection
- ES6语法总结--下篇(进阶篇 ES6~ES11)
- Basic operations of databases and tables ----- classification of data
- 单片机蓝牙无线烧录
- Important methods of array and string
- Mysqldump error1066 error solution
- 2022.2.12 resumption
- Several declarations about pointers [C language]
- 基于Redis的分布式ID生成器
- JS 函数提升和var变量的声明提升
猜你喜欢
JS数组常用方法的分类、理解和运用
Kconfig Kbuild
Amba, ahb, APB, Axi Understanding
Basic operations of databases and tables ----- modifying data tables
JS variable types and common type conversions
ESP8266连接onenet(旧版MQTT方式)
Vulnhub target: hacknos_ PLAYER V1.1
Pytorch: tensor operation (I) contiguous
Classification, understanding and application of common methods of JS array
MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解
随机推荐
ESP8266连接onenet(旧版MQTT方式)
ES6语法总结--下篇(进阶篇 ES6~ES11)
Basic operations of databases and tables ----- creating data tables
Kconfig Kbuild
Basic operations of databases and tables ----- view data tables
MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解
编译原理:源程序的预处理及词法分析程序的设计与实现(含代码)
Working principle of genius telephone watch Z3
Variable parameter principle of C language function: VA_ start、va_ Arg and VA_ end
[leetcode19]删除链表中倒数第n个结点
2021.11.10汇编考试
RuntimeError: cuDNN error: CUDNN_STATUS_NOT_INITIALIZED
Learning notes of JS variable scope and function
JS變量類型以及常用類型轉換
Feature of sklearn_ extraction. text. CountVectorizer / TfidVectorizer
基于Redis的分布式ID生成器
E-commerce data analysis -- salary prediction (linear regression)
Cannot change version of project facet Dynamic Web Module to 2.3.
Rough analysis of map file
Use of lists