Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e7dd9f3

Browse files
add q316
1 parent f65525f commit e7dd9f3

File tree

3 files changed

+62
-22
lines changed

3 files changed

+62
-22
lines changed

‎.idea/workspace.xml‎

Lines changed: 33 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656

5757
* [q20_有效的括号](/src/栈相关/q20_有效的括号)
5858
* [q224_基本计算器](/src/栈相关/q224_基本计算器)
59+
* [q316_去除重复字母](/src/栈相关/q316_去除重复字母)
5960

6061
### 堆相关
6162

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package 栈相关.q316_去除重复字母;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* 栈操作 o(n*log(n))
7+
*/
8+
public class Solution {
9+
10+
public String removeDuplicateLetters(String s) {
11+
Stack<Character> stack = new Stack<>();
12+
for (int i = 0; i < s.length(); i++) {
13+
Character c = s.charAt(i);
14+
if (stack.contains(c)) {
15+
continue;
16+
}
17+
while (!stack.isEmpty() && stack.peek() > c && s.indexOf(stack.peek(), i) != -1) {
18+
stack.pop();
19+
}
20+
stack.push(c);
21+
}
22+
String rs = "";
23+
for (int i = 0; i < stack.size(); i++) {
24+
rs += stack.get(i);
25+
}
26+
return rs;
27+
}
28+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /