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

推荐订阅源

Y
Y Combinator Blog
宝玉的分享
宝玉的分享
月光博客
月光博客
小众软件
小众软件
Jina AI
Jina AI
WordPress大学
WordPress大学
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tailwind CSS Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 【当耐特】
博客园 - 三生石上(FineUI控件)
博客园 - 司徒正美
大猫的无限游戏
大猫的无限游戏
The Cloudflare Blog
G
Google Developers Blog
M
MIT News - Artificial intelligence
N
Netflix TechBlog - Medium
云风的 BLOG
云风的 BLOG
MyScale Blog
MyScale Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
爱范儿
爱范儿
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Blog — PlanetScale
Blog — PlanetScale

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;
}
}