Skip to content

Commit 6b8829a

Browse files
author
binbin.hou
committed
[Feature] add for new
1 parent a6f30ba commit 6b8829a

10 files changed

+185
-10
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/posts/leetcode/2020-06-06-algorithm-000-index-01-overview.md renamed to src/posts/leetcode/index/2020-06-06-algorithm-000-index-01-overview.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
title: 从零开始的数据结构与算法-00-概览 算法专题汇总
44
date: 2020-06-08
5-
categories: [Algorithm]
6-
tags: [algorithm, data-struct, topics, sf]
5+
categories: [Index]
6+
tags: [index]
77
published: true
88
---
99

src/posts/leetcode/2020-06-06-algorithm-000-index-02-how-to-learn.md renamed to src/posts/leetcode/index/2020-06-06-algorithm-000-index-02-how-to-learn.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
title: 从零开始的数据结构与算法-02-如何系统有效的学习数据结构与算法?
44
date: 2020-06-08
5-
categories: [Algorithm]
6-
tags: [algorithm, data-struct, topics, sf]
5+
categories: [Index]
6+
tags: [index]
77
published: true
88
---
99

src/posts/leetcode/2020-06-06-algorithm-000-index-03-topics.md renamed to src/posts/leetcode/index/2020-06-06-algorithm-000-index-03-topics.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22

33
title: 从零开始的数据结构与算法-02-leetcode 算法系统分类
44
date: 2020-06-08
5-
categories: [Algorithm]
6-
tags: [algorithm, data-struct, topics, sf]
5+
categories: [Index]
6+
tags: [index]
77
published: true
88
---
99

10-
#
11-
12-
1310

1411
# chat
1512

src/posts/leetcode/2025-08-22-how-to-cross-leetcodes.md renamed to src/posts/leetcode/index/2025-08-22-how-to-cross-leetcodes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: 分享|如何科学刷题? by 灵茶山艾府
33
date: 2025-08-22
44
categories: [Althgorim]
5-
tags: [althgorim, sh]
5+
tags: [althgorim]
66
published: true
77
---
88

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
---
2+
title: LC2390. 从字符串中移除星号 removing-stars-from-a-string
3+
date: 2025-08-31
4+
categories: [Leetcode-75]
5+
tags: [leetcode, Leetcode-75, sliding-window]
6+
published: true
7+
---
8+
9+
# LC1732. 找到最高海拔 find-the-highest-altitude
10+
11+
给你一个包含若干星号 * 的字符串 s 。
12+
13+
在一步操作中,你可以:
14+
15+
选中 s 中的一个星号。
16+
移除星号 左侧 最近的那个 非星号 字符,并移除该星号自身。
17+
返回移除 所有 星号之后的字符串。
18+
19+
注意:
20+
21+
生成的输入保证总是可以执行题面中描述的操作。
22+
可以证明结果字符串是唯一的。
23+
24+
25+
示例 1:
26+
27+
```
28+
输入:s = "leet**cod*e"
29+
输出:"lecoe"
30+
解释:从左到右执行移除操作:
31+
- 距离第 1 个星号最近的字符是 "leet**cod*e" 中的 't' ,s 变为 "lee*cod*e" 。
32+
- 距离第 2 个星号最近的字符是 "lee*cod*e" 中的 'e' ,s 变为 "lecod*e" 。
33+
- 距离第 3 个星号最近的字符是 "lecod*e" 中的 'd' ,s 变为 "lecoe" 。
34+
不存在其他星号,返回 "lecoe" 。
35+
```
36+
37+
示例 2:
38+
39+
```
40+
输入:s = "erase*****"
41+
输出:""
42+
解释:整个字符串都会被移除,所以返回空字符串。
43+
```
44+
45+
提示:
46+
47+
1 <= s.length <= 10^5
48+
s 由小写英文字母和星号 * 组成
49+
s 可以执行上述操作
50+
51+
52+
# v1-栈
53+
54+
## 思路
55+
56+
我们直接遍历字符串,然后 char 不是 `*` 入栈。
57+
58+
遇到 `*` 的时候,弹出顶部字符。
59+
60+
最后 stack 弹出构建,reverse 一下即可。
61+
62+
## 实现
63+
64+
```java
65+
public String removeStars(String s) {
66+
Stack<Character> stack = new Stack<>();
67+
68+
char[] chars = s.toCharArray();
69+
for(char c : chars) {
70+
if(c == '*') {
71+
if(!stack.isEmpty()){
72+
stack.pop();
73+
}
74+
} else {
75+
stack.push(c);
76+
}
77+
}
78+
79+
// 结果构建
80+
StringBuilder sb = new StringBuilder();
81+
while(!stack.isEmpty()) {
82+
char c = stack.pop();
83+
sb.append(c);
84+
}
85+
return sb.reverse().toString();
86+
}
87+
```
88+
89+
## 效果
90+
91+
103ms 击败 33.83%
92+
93+
## 反思
94+
95+
按理说是可以更快地。
96+
97+
那就是我们不用 stack?
98+
99+
# v2-不用 stack
100+
101+
## 思路
102+
103+
直接用 StringBuilder
104+
105+
如果是 `*`,移除最后一个;如果不是,则 append。其他不变。
106+
107+
## 实现
108+
109+
```java
110+
public String removeStars(String s) {
111+
// 结果构建
112+
StringBuilder sb = new StringBuilder();
113+
114+
char[] chars = s.toCharArray();
115+
for(char c : chars) {
116+
if(c == '*') {
117+
int len = sb.length();
118+
if(len > 0){
119+
sb.deleteCharAt(len-1);
120+
}
121+
} else {
122+
sb.append(c);
123+
}
124+
}
125+
126+
return sb.toString();
127+
}
128+
```
129+
130+
## 效果
131+
132+
38ms 击败 50.23%
133+
134+
## 反思
135+
136+
如何更快呢?
137+
138+
此时问 AI,可以发现 AI 本身认为这已经是最优。
139+
140+
说明还是缺少创造力啊,你个小AI。
141+
142+
# V3-更进一步
143+
144+
##
145+
146+
v1 的栈,并不是算法中的最优解。
147+
148+
最快的方式其实是用数组模拟 stack。
149+
150+
## 实现
151+
152+
```java
153+
public String removeStars(String s) {
154+
char[] chars = s.toCharArray();
155+
// 栈顶
156+
int top = 0;
157+
for(int i = 0; i < s.length(); i++) {
158+
char c = chars[i];
159+
if(c == '*') {
160+
top--;
161+
} else {
162+
chars[top++] = c;
163+
}
164+
}
165+
166+
return new String(chars, 0, top);
167+
}
168+
```
169+
170+
## 效果
171+
172+
13ms 击败 95.07%
173+
174+
## 反思
175+
176+
利用数字模拟实现常见的数据结构,性能一般是最好的。
177+
178+
# 参考资料

0 commit comments

Comments
 (0)