1 class Solution: 2 def convertToAlpha(self,num): 3 return chr(num+96) 4 5 def freqAlphabets(self, s: str) -> str: 6 n = len(s) 7 q = [] 8 i = n - 1 9 while i >= 0: 10 cur = s[i] 11 if cur == '#': 12 num = int(s[i-2:i]) 13 q.insert(0,self.convertToAlpha(num)) 14 i -= 3 15 else: 16 num = int(s[i]) 17 q.insert(0,self.convertToAlpha(num)) 18 i -= 1 19 return ''.join(q)
倒序遍历,遇到#则读取前2位,并转换位字母,否则读取当前位转换位字母。