当前位置:网站首页>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
边栏推荐
- R语言ggplot2可视化:使用patchwork包的plot_layout函数将多个可视化图像组合起来,nrow参数指定行的个数、byrow参数指定按照列顺序排布图
- 【Gazebo入门教程】第二讲 模型库导入与可视化机器人建模(模型编辑器)
- Codeforces Round #811 (Div. 3)
- 一张图片怎么旋转90度。利用ps
- 西西成语接龙小助手
- Liunx删除乱码文件
- C# Sqlite database construction and use skills
- yarn detailed introductory tutorial
- Boost library study notes (1) Installation and configuration
- Nacos集群搭建
猜你喜欢

对象实例化之后一定会存放在堆内存中?

mysqlbinlog 超过500g自动删除,保留7个,求大深给个版本
C# Sqlite database construction and use skills

Matlab画图1

租房小程序登顶码云热门

谷歌开发者社区推荐:《Jetpack Compose 从入门到实战》新书上架,带你踏上 Compose 开发之旅~

集群监控——Zabbix使用

DSPE-PEG-DBCO,DBCO-PEG-DSPE,磷脂-聚乙二醇-二苯并环辛炔科研实验用

Boost library study notes (1) Installation and configuration

】 【 LeetCode daily one problem - 540. The order of a single element of the array
随机推荐
R语言ggplot2可视化:使用patchwork包的plot_layout函数将多个可视化图像组合起来,nrow参数指定行的个数、byrow参数指定按照列顺序排布图
最小区间覆盖
【技术笔记】树莓派4B开机流程整理(无显示器安装)
企业调查相关性分析案例
localhost,127.0.0.1,本机IP
WPF 修改 ItemContainerStyle 鼠标移动到未选中项效果和选中项背景
树莓派温度监视关机保护脚本
clickhouse 上下线表
R语言使用ggpubr包的ggsummarystats函数可视化柱状图(通过ggfunc参数设置)、在可视化图像的下方添加描述性统计结果表格、palette参数配置柱状图及统计数据的颜色
Digital-intelligent supply chain management system for chemical manufacturing industry: build a smart supply system and empower enterprises to improve production efficiency
2022年7月31日 暑假第三周总结
SRM供应商协同管理系统功能介绍
(1), the sequential storage structure of linear table chain storage structure
Cron表达式
Qt自动补全之QCompleter使用
arm交叉编译
MySQL学习笔记-4.数据更新时的性能问题
知乎高赞:拼多多和国家电网,选哪个?
CAS:385437-57-0,DSPE-PEG-Biotin,生物活性分子磷脂-聚乙二醇-生物素
Learning and Exploration-Introducing Baidu Statistics to the Website