Golang | Leetcode Golang题解之第316题去除重复字母

avatar
作者
筋斗云
阅读量:0

题目:

题解:

func removeDuplicateLetters(s string) string {     left := [26]int{}     for _, ch := range s {         left[ch-'a']++     }     stack := []byte{}     inStack := [26]bool{}     for i := range s {         ch := s[i]         if !inStack[ch-'a'] {             for len(stack) > 0 && ch < stack[len(stack)-1] {                 last := stack[len(stack)-1] - 'a'                 if left[last] == 0 {                     break                 }                 stack = stack[:len(stack)-1]                 inStack[last] = false             }             stack = append(stack, ch)             inStack[ch-'a'] = true         }         left[ch-'a']--     }     return string(stack) }

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!