当前位置:网站首页>[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);
}
}
边栏推荐
- GCC compilation options
- ES6 grammar summary -- Part 2 (advanced part es6~es11)
- The dolphin scheduler remotely executes shell scripts through the expect command
- E-commerce data analysis -- salary prediction (linear regression)
- Priority inversion and deadlock
- Flink late data processing (3)
- Variable parameter principle of C language function: VA_ start、va_ Arg and VA_ end
- (一)R语言入门指南——数据分析的第一步
- 1081 rational sum (20 points) points add up to total points
- RuntimeError: cuDNN error: CUDNN_ STATUS_ NOT_ INITIALIZED
猜你喜欢

【ESP32学习-2】esp32地址映射

JS数组常用方法的分类、理解和运用

(1) Introduction Guide to R language - the first step of data analysis
![[esp32 learning-1] construction of Arduino esp32 development environment](/img/31/dc16f776b7a95a08d177b1fd8856b8.png)
[esp32 learning-1] construction of Arduino esp32 development environment

2021.11.10汇编考试

MySQL time, time zone, auto fill 0

Navigator object (determine browser type)

ESP learning problem record

Conditional probability

level16
随机推荐
JS變量類型以及常用類型轉換
(五)R语言入门生物信息学——ORF和序列分析
SSD technical features
基于Redis的分布式ID生成器
Basic operations of databases and tables ----- creating data tables
Types de variables JS et transformations de type communes
2022.2.12 resumption
E-commerce data analysis -- salary prediction (linear regression)
Vulnhub target: hacknos_ PLAYER V1.1
[esp32 learning-1] construction of Arduino esp32 development environment
[leetcode622]设计循环队列
MySQL時間、時區、自動填充0的問題
Problèmes avec MySQL time, fuseau horaire, remplissage automatique 0
Minio文件下载问题——inputstream:closed
The dolphin scheduler remotely executes shell scripts through the expect command
How to add music playback function to Arduino project
1081 rational sum (20 points) points add up to total points
MySQL占用内存过大解决方案
[899]有序队列
map文件粗略分析