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

推荐订阅源

H
Hackread – Cybersecurity News, Data Breaches, AI and More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
V2EX
T
The Blog of Author Tim Ferriss
腾讯CDC
Hugging Face - Blog
Hugging Face - Blog
雷峰网
雷峰网
爱范儿
爱范儿
GbyAI
GbyAI
H
Help Net Security
I
InfoQ
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
人人都是产品经理
人人都是产品经理
J
Java Code Geeks
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
N
Netflix TechBlog - Medium
Last Week in AI
Last Week in AI
宝玉的分享
宝玉的分享
云风的 BLOG
云风的 BLOG
Project Zero
Project Zero
P
Privacy & Cybersecurity Law Blog
A
Arctic Wolf
Know Your Adversary
Know Your Adversary
G
Google Developers Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tor Project blog
V
Vulnerabilities – Threatpost
Y
Y Combinator Blog
WordPress大学
WordPress大学
V
Visual Studio Blog
博客园_首页
G
GRAHAM CLULEY
K
Kaspersky official blog
T
Tailwind CSS Blog
T
Threat Research - Cisco Blogs
博客园 - Franky
D
Docker
Security Latest
Security Latest
I
Intezer
有赞技术团队
有赞技术团队
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 【当耐特】
B
Blog RSS Feed
T
The Exploit Database - CXSecurity.com
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

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