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的次方,那么二进制表达式为...
Output: false classSolution {publicbooleanisPowerOfTwo(intn) {if(n<0){returnfalse; } System.out.println(n);if(n==0){returnfalse; }//boolean flag = false;intres;while(n >=2){ res= n%2;if(res !=0){returnfalse; } n= n/2; }returntrue; } }...
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 满足...
2)Power of 3. 是否为3的幂指数 Loading...leetcode.com/problems/power-of-three/ 2.1)采用循环 class Solution: def isPowerOfThree(self, n: int) -> bool: if not n: return False while n: if n==1: return True if n%3: return False n //=3 return True ...
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
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
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
Leetcode 231 2的幂 书森学院 65 1 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 一周刷爆Le...
leetcode231. Power of Two 题目要求 Given an integer, write a function to determine if it is a power of two. 判断一个整数是否是2的幂。 思路和代码 当我们从二进制的角度来看,这个题目就非常简单了。其实题目的要求等价于该整数对应的二进制数中,一共有几个1。该题目的难点在于考虑边界情况,比如-2...
就是判断一个数是不是2的次方数。 代码如下: public class Solution { public boolean isPowerOfTwo(int n) { if(n<=0) return false; n = n&(n-1); return n==0 ?true:false; } } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. ...