如果存到一个小的就把比它大的都pop出去,在后面还有的情况下,如果后面没有就不pop了
class Solution {public String removeDuplicateLetters(String s) {Stack<Character> stack=new Stack<>();int[] count=new int[256];boolean[] inStack=new boolean[256];//记录是否在栈中for(char c:s.toCharArray()){count[c]++;//记录出现次数}for(char x:s.toCharArray()){//记得调整 instack数组!count[x]--;//处理中减一个if(inStack[x]){continue;}while(!stack.isEmpty()&&x<stack.peek()){//如果新的x小,则pop之前的(后面未出现的if(count[stack.peek()]==0){break;}inStack[stack.pop()] = false;//若之后还有}stack.push(x);inStack[x]=true;}StringBuilder sb=new StringBuilder();while(!stack.isEmpty()){char c=stack.pop();sb.append(c);}return sb.reverse().toString();}
}