当前位置:网站首页>LeetCode 899. Ordered Queues
LeetCode 899. Ordered Queues
2022-08-04 17:33:00 【Big bad wolf】
题目描述
给定一个字符串 s 和一个整数 k .你可以从 s 的前 k 个字母中选择一个,并把它加到字符串的末尾.
返回 在应用上述步骤的任意数量的移动后,字典上最小的字符串 .
思路
It was relatively simple at first,从前k个位置中,Just keep moving the first character of the reversed pair to the last position.
So you will see such a use case:
“baaca”
2
Use the above strategy,得到的答案是:aacab
,The actual answer is yesaaabc
.
容易发现,当k >= 2
时,The original string can be converted,Perform arbitrary transformations,That is to say, you can get any string,Then, of course, you can also get the string with the smallest lexicographical order.
在k = 2
时,我们可以交换字符串中的Any two adjacent characters,and leave the rest of the characters unchanged
比如原字符串为1234ab56789
,We want to exchangeab
Then you can continuously move the first character to the end,直到ab
appears in the first
ab567891234
随后我们将a
移到末尾,得到b567891234a
接着,将b
The last bit is constantly swapped to the end(使得a
不断往前走),得到ba567891234
再将ba
Rotate to the original position(Keep moving the first character to the end),得到1234ba56789
This achieves the exchangeab
两个字符.
同理,We can keep the rest of the characters in the same position,Swap any two other adjacent characters.
As long as we can swap any two adjacent characters,Bubble sort can be achieved.
于是,在k = 2
时,We always get sorted strings,That is, the string with the smallest lexicographical order is always obtained.
对于k > 2
,就更不用说了.
而在k = 1
时,We can only swap the first character to the end at a time,A string that can be constructed,一共只有n
种情况(n
是字符串的长度,Every character can act as the first character,所以共n
种).
代码
The idea came up,The code is very easy to write.The difficulty is in thinking.
We just need to categorize and discuss,k = 1
时,Rotate the entire string,Find the one with the smallest lexicographical order;k >= 2
时,Sort the strings lexicographically.
class Solution {
public String orderlyQueue(String s, int k) {
int n = s.length();
if (k == 1) {
String ans = s;
for (int i = 0; i < n; i++) {
s = s.substring(1) + s.charAt(0);
if (ans.compareTo(s) > 0) ans = s;
}
return ans;
}
// k = 2
char[] chars = s.toCharArray();
Arrays.sort(chars);
return new String(chars);
}
}
时间复杂度 : O ( n 2 ) O(n^2) O(n2)
- 在
k = 1
need to enumeraten
个可能的字符串,And the comparison of each string is required O ( n ) O(n) O(n)的时间,所以一共是 O ( n 2 ) O(n^2) O(n2).当然,这个过程可以使用字符串的最小表示法,优化到 O ( n ) O(n) O(n) - 在
k >= 2
need to be sorted,复杂度 O ( n l o g n ) O(nlogn) O(nlogn),最坏 O ( n 2 ) O(n^2) O(n2)
空间复杂度: O ( n ) O(n) O(n)
在k = 1
need to open up additional space to store all possible strings(因为JavaStrings in are immutable objects)
扩展
字符串的最小表示法:Find the smallest lexicographical order that can be obtained by rotating a string
TODO
边栏推荐
猜你喜欢
随机推荐
一张图片怎么旋转90度。利用ps
对象实例化之后一定会存放在堆内存中?
Boost library study notes (1) Installation and configuration
安装失败怎么办
图扑软件与华为云共同构建新型智慧工厂
小程序学习目标
在VMD上可视化hdf5格式的分子轨迹文件
树莓派连接蓝牙音箱
华为云计算HCIE之oceanstor仿真器的安装教程
软件测试高频面试题真实分享/网上银行转账是怎么测的,设计一下测试用例。
SRM Supplier Collaborative Management System Function Introduction
语音识别学习资源
Catering Supply Chain Management System
el-date-picker 设置时间范围
并发编程原理学习-reentrantlock源码分析
Liunx删除乱码文件
2022年五一数学建模C题讲解
【日记】nodejs构建API框架以及RESTful API 和 JSON-RPC的取舍
两个对象相同数据赋值
Compose 类型稳定性注解:@Stable & @Immutable