当前位置:网站首页>[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);
}
}
边栏推荐
- 基于Redis的分布式锁 以及 超详细的改进思路
- (4) Data visualization of R language -- matrix chart, histogram, pie chart, scatter chart, linear regression and strip chart
- Classification, understanding and application of common methods of JS array
- MySQL占用内存过大解决方案
- dosbox第一次使用
- js 变量作用域和函数的学习笔记
- VIM command line notes
- 2021.11.10汇编考试
- Variable parameter principle of C language function: VA_ start、va_ Arg and VA_ end
- JS正则表达式基础知识学习
猜你喜欢
History object
【ESP32学习-2】esp32地址映射
(五)R语言入门生物信息学——ORF和序列分析
Mp3mini playback module Arduino < dfrobotdfplayermini H> function explanation
Walk into WPF's drawing Bing Dwen Dwen
Learning notes of JS variable scope and function
ESP学习问题记录
C language callback function [C language]
Priority inversion and deadlock
單片機藍牙無線燒錄
随机推荐
2022.2.12 resumption
Basic operations of databases and tables ----- classification of data
Use of lists
Kaggle competition two Sigma connect: rental listing inquiries
[offer9]用两个栈实现队列
js 变量作用域和函数的学习笔记
JS Title: input array, exchange the largest with the first element, exchange the smallest with the last element, and output array.
Intermediate use tutorial of postman [environment variables, test scripts, assertions, interface documents, etc.]
Esp8266 connect onenet (old mqtt mode)
Dead loop in FreeRTOS task function
Rough analysis of map file
Arduino uno R3 register writing method (1) -- pin level state change
ORA-02030: can only select from fixed tables/views
History object
Detailed explanation of Union [C language]
Common properties of location
C language callback function [C language]
如何给Arduino项目添加音乐播放功能
JS變量類型以及常用類型轉換
Redis 缓存更新策略,缓存穿透、雪崩、击穿问题