classSolution: defcountAndSay(self, n): result = '' start_str = '' for i in range(n): num = 0 if i == 0: start_str = '1' continue for j in range(len(start_str)): num += 1 if j< len(start_str)-1and start_str[j] == start_str[j+1]: continue else: # 当start_str[j] != start_str[j+1] 或者j=len(start_str)-1时,num肯定也是为1的 result = result + str(num) + start_str[j] num = 0 start_str = result result = '' return start_str
classSolution: defcountAndSay(self, n): if n == 1: return'1' start_str = self.countAndSay(n-1) num = 0 result = '' for j in range(len(start_str)): num += 1 if j< len(start_str)-1and start_str[j] == start_str[j+1]: continue else: # 当start_str[j] != start_str[j+1] 或者j=len(start_str)-1时,num肯定也是为1的 result = result + str(num) + start_str[j] num = 0