解题思路:
1:获取n数字的长度下所有2的幂的数字(例如3,则获取2,4,8)
2:获取步骤1中每个数字的0-9的数量,并组合成字符串,添加到集合list中
3:获取数字n每个数字的0-9的数量,并组合成字符串判断是否存在于list中即可
class Solution: def reorderedPowerOf2(self, n: int) -> bool: if n == 0: return False # 数字n的长度 n_len = len(str(n)) max = 10 ** n_len - 1 min = 10 ** (n_len - 1) # com_par用于保存位数在n-1 到 n之间所有的2的幂次方的数 # 保存的结构为字符串,方便后续处理 com_par = [] a = 1 for i in range(30): if a >= min: if a <= max: com_par.append(self.get_per_num(a)) else: break a = a * 2 dest = self.get_per_num(n) return dest in com_par def get_per_num(self, n) -> str: """ :param n: :return: n中0-9数字分别的数量 """ reuslt = [0 for i in range(10)] while n > 0: reuslt[n % 10] += 1 n = int(n / 10) return str(reuslt) if __name__ == '__main__': print(1 << 2) s = 2 # s = "()())()" solution = Solution() of_ = solution.reorderedPowerOf2(s) print(of_)