1370. Increasing Decreasing String 链接到标题 题目描述很复杂,但其实就是排序和字典,先顺序遍历再反向遍历。(熟练使用 Counter)
Python 链接到标题 class Solution(object): def sortString(self, s): """ :type s: str :rtype: str """ set_s = set() for c in s: set_s.add(c) characters = sorted(set_s) d = collections.Counter(s) res = "" while sum(d.values()) != 0: for c in characters: if c in s and d[c] > 0: res += c d[c] -= 1 for c in characters[::-1]: if c in s and d[c] > 0: res += c d[c] -= 1 return res Golang 链接到标题 func sortString(s string) string { sMap := make(map[string]int, len(s)) for _, c := range s { sMap[string(c)]++ } chars := []string{} for c, _ := range sMap { chars = append(chars, string(c)) } sort.