Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Example: Given"bcabc"Return"abc"Given"cbacdcbc"Return"acdb" 分析:通...
这样最后stack里面就是字符序的possible result 1publicclassSolution {2publicString removeDuplicateLetters(String s) {3if(s==null|| s.length()==0)return"";4Stack<Character> st =newStack<Character>();5StringBuffer sb =newStringBuffer();6boolean[] visited =newboolean[26];7int[] count =newin...
代码(Go) func removeDuplicateLetters(s string) string { // lastIndex[ch] 表示 ch 在 s 中的最后一个出现的位置 lastIndex := make(map[rune]int) // 带下标遍历 s 中的字符 for i, ch := range s { // 更新每个字符最后一次出现的位置 lastIndex[ch] = i } // isInStack[ch] 表示 ch ...
Can you solve this real interview question? Remove Duplicate Letters - Given a string s, remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smallest in lexicographical order among all possible
/** * @param {string} s * @return {string} */ var removeDuplicateLetters = function(s) { // 存放字符剩余出现的次数,会随着s的遍历逐渐减少 // key是字符,value是每个字符出现的次数 let countMap = {}; for (let c of s) { countMap[c] = countMap[c] ? countMap[c] + 1 : 1; ...
leetcode刷题指南之RemoveDuplicateLetters。暴力的做法只考虑了前一个条件,却没有考虑字典序最小这个也很关键的条件。如果没有,考虑s的最后一个字母,如果它比最后第i位小,并且在第i位之后还出现了,那幺就删掉,直到s为空或者s的最后一个字母不满足条件,把第i位放到最
public String removeDuplicateLetters(String s) { if(s==null || s.length()<=0) return ""; else { int[] count=new int[26]; for(int i=0;i<s.length();i++) count[s.charAt(i)-'a']++; int pos=0; for(int i=0;i<s.length();i++) ...
一个人刷题很难,大家一起刷题就不难了。Leetcode 316 是一道用到贪心算法和栈的中等难度题,本期视频由大叔为大家讲解思路,并演示算法,源代码请在我们的github账号上查询题号:https://github.com/dashu-xiaobai/leetcode/ 喜欢我们的朋友,欢迎你们的点赞关注与订阅!,
classSolution:defremoveDuplicateLetters(self,s)->int:stack=[]remain_counter=collections.Counter(s)forcins:ifcnotinstack:whilestackandc<stack[-1]andremain_counter[stack[-1]]>0:stack.pop()stack.append(c)remain_counter[c]-=1return''.join(stack) ...
leetcode 每日一题:316. 去除重复字母:https://leetcode-cn.com/problems/remove-duplicate-letters/ 一起刷题吧 一、题目分析 输入:字符串输出:去重后的字符串,要求保留原有字符的相对顺序,且得到的字符串最小难度:中等标签:栈、贪心 示例1: 输入:s = "bcabc" 输出:"abc" ...