惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
人人都是产品经理
人人都是产品经理
aimingoo的专栏
aimingoo的专栏
M
MIT News - Artificial intelligence
Recent Announcements
Recent Announcements
V
Visual Studio Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
MyScale Blog
MyScale Blog
Hugging Face - Blog
Hugging Face - Blog
宝玉的分享
宝玉的分享
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 叶小钗
博客园 - 聂微东
U
Unit 42
F
Fortinet All Blogs
Microsoft Security Blog
Microsoft Security Blog
GbyAI
GbyAI
IT之家
IT之家
The GitHub Blog
The GitHub Blog
Stack Overflow Blog
Stack Overflow Blog
MongoDB | Blog
MongoDB | Blog
Y
Y Combinator Blog
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)

Bluemangoo's Blog

看一点答辩Issue 在 Vercel 上部署 MCSManager 的 UI 项目 Python 语言基础 Vercel 折腾笔记 建站一周年纪念! Bing Points 辅助工具 白象过湾 Windows 10 触摸键盘大小调整 尘烟 One一个 文章推荐 内存填充器 Markdown 加载器 【芒果快评】安倍晋三遭枪击 常用软件下载链接 那些震惊了我的文案(持续更新) 一个抛鸡蛋的小游戏 《1984》书评 Explorer Patcher 推荐 英语书也会出错啊 图片转Python的turtle库代码 Phigros电脑版 Windows 11 子系统(WSA)安装方法
力扣 0899 - 有序队列
Bluemangoo · 2022-08-03 · via Bluemangoo's Blog

思路

脑筋急转弯啊!

当 k >= 2 时一定可以排出来,直接排序字符串就可以。

当 k == 1 时遍历一遍找最小就行。

我做了一个优化:先把字符串里面的最小字符找出来,然后再遍历这几个字符串找最小。

另外,把 s.length() == 1 的情况直接输出;

s.length() == 2 的情况直接排序。

899.png

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution {
public String orderlyQueue(String s, int k) {
if (k>1||s.length()==2){
char[] ans=s.toCharArray();
Arrays.sort(ans);
return String.valueOf(ans);
}
if (s.length()<2){
return s;
}
int[] n = new int[s.length() + 1];
Arrays.fill(n,-1);
int j = 0;
char c = Character.MAX_VALUE;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) < c) {
j = 0;
Arrays.fill(n,-1);
n[j++] = i;
c=s.charAt(i);
continue;
}
if (s.charAt(i) == c) {
n[j++] = i;
}
}
String ans = null;
for (int i=0;i<n.length&&n[i]!=-1;i++) {
String now = s.substring(n[i]) + s.substring(0, n[i]);
if (ans == null) {
ans = now;
continue;
}
if (ans.compareTo(now) > 0) {
ans = now;
}
}
return ans;
}
}