题解 | #牛牛的特殊数字#
牛牛的特殊数字
https://www.nowcoder.com/practice/2f195566eb5442e883c371d8cfee8c77
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param num int整型 # @return bool布尔型 # class Solution: def isSpecialNumber(self, num: int) -> bool: # write code here num = list(map(int, str(num))) self.bit_sum = [] self.key = False def get_bit_sum(start): if self.key: return if start == len(num): self.key = judge(self.bit_sum) return self.bit_sum.append(num[start]) get_bit_sum(start + 1) self.bit_sum.pop() if not self.bit_sum: return if len(self.bit_sum) > 1 or self.bit_sum[0] > 1: self.bit_sum[-1] -= 1 self.bit_sum.append(10 + num[start]) get_bit_sum(start + 1) self.bit_sum.pop() self.bit_sum[-1] += 1 elif self.bit_sum[0] == 1: self.bit_sum[0] = 10 + num[start] get_bit_sum(start + 1) self.bit_sum[0] = 1 return def judge(num: list): if num == 0: return True if num == num[::-1]: if len(num) % 2 == 0: return True elif num[len(num) // 2] % 2 == 0: return True return False get_bit_sum(0) return self.key