LeetCode 869 Reordered Power of 2 (Python)

Posted by 小明MaxMing on March 21, 2021

题目

Starting with a positive integer N, we reorder the digits in any order (including the original order) such that the leading digit is not zero.

Return true if and only if we can do this in a way such that the resulting number is a power of 2.

解题思路

降所有2的幂转换成字符串然后排序,对N排序,判断是否在2的幂中

代码

class Solution:
    def reorderedPowerOf2(self, N: int) -> bool:
        pow2 = [''.join(sorted(list(str(2**i)))) for i in range(31)]
        return ''.join(sorted(list(str(N)))) in pow2

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅