We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
给你一个字符串 s ,请你去除字符串中重复的字母,使得每个字母只出现一次。需保证 返回结果的字典序最小(要求不能打乱其他字符的相对位置)。 注意:该题与 1081 https://leetcode-cn.com/problems/smallest-subsequence-of-distinct-characters 相同 示例 1: 输入:s = "bcabc" 输出:"abc" 示例 2: 输入:s = "cbacdcbc" 输出:"acdb" 提示: 1 <= s.length <= 104 s 由小写英文字母组成
给你一个字符串 s ,请你去除字符串中重复的字母,使得每个字母只出现一次。需保证 返回结果的字典序最小(要求不能打乱其他字符的相对位置)。
注意:该题与 1081 https://leetcode-cn.com/problems/smallest-subsequence-of-distinct-characters 相同
示例 1:
输入:s = "bcabc" 输出:"abc" 示例 2:
输入:s = "cbacdcbc" 输出:"acdb"
提示:
1 <= s.length <= 104 s 由小写英文字母组成
javascript
/** * @param {string} s * @return {string} */ var removeDuplicateLetters = function(s) { var map = new Map(), stack = []; for(var i = 0, length = s.length; i < length; i++) { var current = s[i]; if (map.get(current)) { map.set(current, map.get(current) + 1); } else { map.set(current, 1); } } for (var i = 0, length = s.length; i < length; i++) { var current = s[i]; map.set(current, map.get(current) - 1); if (stack.includes(current) == false) { var j = stack.length; while(j-- && stack[j] > current && map.get(stack[j])) { stack.pop(); } stack.push(current); } } return stack.join(''); };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
习题
思路
解答
javascript
The text was updated successfully, but these errors were encountered: