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

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Recent Announcements
Recent Announcements
博客园 - 【当耐特】
博客园 - 三生石上(FineUI控件)
量子位
aimingoo的专栏
aimingoo的专栏
V
V2EX
Vercel News
Vercel News
B
Blog
M
MIT News - Artificial intelligence
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
TaoSecurity Blog
TaoSecurity Blog
N
News and Events Feed by Topic
D
DataBreaches.Net
Blog — PlanetScale
Blog — PlanetScale
S
Secure Thoughts
U
Unit 42
博客园 - 叶小钗
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
N
News | PayPal Newsroom
Help Net Security
Help Net Security
S
Security Affairs
Microsoft Security Blog
Microsoft Security Blog
W
WeLiveSecurity
博客园 - Franky
Forbes - Security
Forbes - Security
Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页
Schneier on Security
Schneier on Security
I
InfoQ
B
Blog RSS Feed
大猫的无限游戏
大猫的无限游戏
A
About on SuperTechFans
Webroot Blog
Webroot Blog
AWS News Blog
AWS News Blog
Last Week in AI
Last Week in AI
Security Archives - TechRepublic
Security Archives - TechRepublic
C
CERT Recently Published Vulnerability Notes
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
L
Lohrmann on Cybersecurity
SecWiki News
SecWiki News
Recent Commits to openclaw:main
Recent Commits to openclaw:main
J
Java Code Geeks

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