classSolution{publicbooleanisPowerOfTwo(intx){returnx >0&& (x ==1|| (x %2==0&& isPowerOfTwo(x /2))); } } Python 实现(递归) classSolution:defisPowerOfTwo(self, x):""" :type n: int :rtype: bool """returnx >0and(x ==1o
publicbooleanisPowerOfTwo(intn) {if(n<0||n==0)returnfalse;intremainder=n%2;ints=n/2;while(remainder==0){//remainder为0时继续做除法 remainder=s%2; s=s/2; }if(s==0)returntrue;//remainder不为0,此时看s是否变成0returnfalse; } 有bit解决方法,一个数如果是2的次方,那么二进制表达式为...
Can you solve this real interview question? Reordered Power of 2 - You are given an 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 so that t
class Solution: def reorderedPowerOf2(self, n: int) -> bool: # 先计算 n 的十进制位出现次数,方便后续复用 digit_to_cnt: Counter = Counter(str(n)) # 枚举所有 2 的幂次方 for i in range(31): # 如果所有十进制数位的出现次数都相同,则 n 和 1 << i 是按十进制位重排的, #则 n 满足...
func isPowerOfTwo(n int) bool { // 非正数一定不是 2 的幂次方 if n <= 0 { return false } return (n & (n - 1)) == 0 } 题目链接: Power of Two: leetcode.com/problems/p 2 的幂: leetcode.cn/problems/po LeetCode 日更第 253 天,感谢阅读至此的你 欢迎点赞、收藏、在看鼓励...
Leetcode力扣239.滑动窗口最大值 丶mars_ 3481 3 Leetcode 231. Power of Two (Python) Myron_yoo 21 0 Leetcode刷题 2 两数相加 Add Two Numbers JS老毕 2257 4 Leetcode力扣22 手画图解版|括号生成Generate Parentheses 爱学习的饲养员 4029 4 一周刷爆LeetCode,算法大神左神(左程云)耗时100天打造...
LeetCode Top Interview Questions 231. Power of Two (Java版; Easy) 题目描述 Given an integer, write a function to determine if it is a power of two. Example 1: Input: 1 Output: true Explanation: 20 = 1 Example 2: Input: 16
题目地址:https://leetcode.com/problems/reordered-power-of-2/description/ 题目描述 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. ...
Can you solve this real interview question? Power of Two - Given an integer n, return true if it is a power of two. Otherwise, return false. An integer n is a power of two, if there exists an integer x such that n == 2x. Example 1: Input: n = 1 Ou
leetcode231. Power of Two 题目要求 Given an integer, write a function to determine if it is a power of two. 判断一个整数是否是2的幂。 思路和代码 当我们从二进制的角度来看,这个题目就非常简单了。其实题目的要求等价于该整数对应的二进制数中,一共有几个1。该题目的难点在于考虑边界情况,比如-2...