当前位置:网站首页>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,直到abappears in the first
ab567891234
随后我们将a移到末尾,得到b567891234a
接着,将bThe last bit is constantly swapped to the end(使得a不断往前走),得到ba567891234
再将baRotate 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 = 1need 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 >= 2need 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 = 1need 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
边栏推荐
- 区间贪心(区间合并)
- 语音识别学习资源
- Matlab画图1
- 对象实例化之后一定会存放在堆内存中?
- 最小区间覆盖
- response的contentType 几种类型
- Digital-intelligent supply chain management system for chemical manufacturing industry: build a smart supply system and empower enterprises to improve production efficiency
- 基于clipboard.js对复制组件的封装
- PT100铂热电阻三种测温方法介绍
- R语言ggplot2可视化:使用patchwork包的plot_layout函数将多个可视化图像组合起来,nrow参数指定行的个数、byrow参数指定按照列顺序排布图
猜你喜欢

浅谈运用低代码技术如何实现物流企业的降本增效

多线程学习笔记-3.并发容器

【LeetCode Daily Question】——374. Guess the size of the number

Boost library study notes (1) Installation and configuration

Flutter实战-请求封装(四)之gzip报文压缩

】 【 LeetCode daily one problem - 540. The order of a single element of the array

Thrift IDL示例文件

【web自动化测试】Playwright快速入门,5分钟上手

学习探索-网站中引入百度统计

yarn detailed introductory tutorial
随机推荐
吃透Chisel语言.32.Chisel进阶之硬件生成器(一)——Chisel中的参数化
JWT主动校验Token是否过期
C# Sqlite database construction and use skills
R语言使用ggpubr包的ggsummarystats函数可视化柱状图(通过ggfunc参数设置)、在可视化图像的下方添加描述性统计结果表格、palette参数配置柱状图及统计数据的颜色
谷歌开发者社区推荐:《Jetpack Compose 从入门到实战》新书上架,带你踏上 Compose 开发之旅~
身为程序员的我们如何卷死别人?破局重生。
CF86D Powerful array
【无标题】
框架整合(二)- 使用Apache ShardingSphere实现数据分片
嵌入式开发:使用堆栈保护提高代码完整性
学习探索-网站中引入百度统计
hi, 请问下这是什么问题, 我看官网的example就是mysql的, 咋提示不支持?
自定义组件,并在组件中注入自定义组件实现多种场景的下的组件切换
软件测试高频面试题真实分享/网上银行转账是怎么测的,设计一下测试用例。
第一章 对象和封装
太一集团全资收购火币旗下社交产品火信
mysqlbinlog 超过500g自动删除,保留7个,求大深给个版本
LeetCode Question of the Day - 1403. Minimum Subsequence in Non-Increasing Order
信息系统项目管理师必背核心考点(六十)项目集管理
JS中null与undefined的异同点