可以通过验证
var removeDuplicateLetters = function (s) {
let stack = []
let index = -1;
//++index
while (++index < s.length) {
if (stack.includes(s[index])) continue;
while (
stack.length &&
stack[stack.length - 1] > s[index] &&
s.includes(stack[stack.length - 1], index)
) {
stack.pop();
}
stack.push(s[index]);
}
return stack.join("");
};
不能通过验证
var removeDuplicateLetters = function (s) {
let stack = []
let index = 0;
//index从0开始,自增写在while里
while (index < s.length) {
if (stack.includes(s[index])) continue;
while (
stack.length &&
stack[stack.length - 1] > s[index] &&
s.includes(stack[stack.length - 1], index)
) {
stack.pop();
}
stack.push(s[index]);
index++;
}
return stack.join("");
};