package 栈相关.q316_去除重复字母;
import java.util.Stack;
/**
* 栈操作 o(n*log(n))
*/
public class Solution {
public String removeDuplicateLetters(String s) {
Stack<Character> stack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
Character c = s.charAt(i);
if (stack.contains(c)) {
continue;
}
while (!stack.isEmpty() && stack.peek() > c && s.indexOf(stack.peek(), i) != -1) {
stack.pop();
}
stack.push(c);
}
String rs = "";
for (int i = 0; i < stack.size(); i++) {
rs += stack.get(i);
}
return rs;
}
}
q316_去除重复字母
作品《LeetCode题目分类与面试问题整理 - q316_去除重复字母》由 不喝星巴克 发布于 匠果,转载请注明出处及链接地址:
http://www.jiangguo.net/c/9r6/8l5.html