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

推荐订阅源

M
MIT News - Artificial intelligence
博客园 - Franky
H
Help Net Security
A
About on SuperTechFans
Know Your Adversary
Know Your Adversary
罗磊的独立博客
Help Net Security
Help Net Security
腾讯CDC
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
Project Zero
Project Zero
有赞技术团队
有赞技术团队
Blog — PlanetScale
Blog — PlanetScale
T
Threat Research - Cisco Blogs
The Hacker News
The Hacker News
Engineering at Meta
Engineering at Meta
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Simon Willison's Weblog
Simon Willison's Weblog
T
Threatpost
Google DeepMind News
Google DeepMind News
V
V2EX
B
Blog
人人都是产品经理
人人都是产品经理
J
Java Code Geeks
N
Netflix TechBlog - Medium
P
Privacy International News Feed
Recorded Future
Recorded Future
D
Darknet – Hacking Tools, Hacker News & Cyber Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Stack Overflow Blog
Stack Overflow Blog
Cisco Talos Blog
Cisco Talos Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Securelist
NISL@THU
NISL@THU
The GitHub Blog
The GitHub Blog
T
Troy Hunt's Blog
S
Security @ Cisco Blogs
Vercel News
Vercel News
L
LINUX DO - 热门话题
博客园_首页
The Register - Security
The Register - Security
GbyAI
GbyAI
TaoSecurity Blog
TaoSecurity Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
V2EX - 技术
V2EX - 技术
L
LangChain Blog
T
Tor Project blog
P
Privacy & Cybersecurity Law Blog
Security Latest
Security Latest
K
Kaspersky official blog

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