运行 AI代码解释 publicclassSolution{publicbooleanisPowerOfFour(int num){// 转化为二进制数来判断if(num<0)returnfalse;String binaryStr=Integer.toBinaryString(num);for(int i=0;i<binaryStr.length();i++){if(i==0&&binaryStr.charAt(i)!='1')returnfalse;elseif(i>0&&binaryStr.charAt(i)!='...
题目地址:https://leetcode.com/problems/power-of-four/ Total Accepted: 9305 Total Submissions: 28083 Difficulty: Easy 题目描述 Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example 1: Input:16Output:true Example 2: Input:5Output:false Follow up...
leetcode 342. Power of Four Given an integer (signed 32 bits), write a function to check whether it is a power of 4.Example: Given num = 16, return true. Given num = 5, return false. Follow up: Could you solve it without loops/recursion?
Leetcode 342 Power of Four Leetcode 342 Power of Four...Power of Four Power of Four Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example Input: 16 Output: true Input: 5 Output: false Solution...Power of Four https://www.lintcode...
LeetCode 题解之 342. Power of Four 342. Power of Four 题目描述和难度 题目描述: 给定一个整数 (32位有符整数型),请写出一个函数来检验它是否是4的幂。 示例: 当num = 16 时 ,返回 true 。当 num = 5时,返回 false。 问题进阶:你能不使用循环/递归来解决这个问题吗?
class Solution: def isPowerOfFour(self, n: int) -> bool: # 4 的幂次方必定满足以下条件:二进制位有且仅有 1 位为 1 ,且该二进制位是偶数位。 # 我们可以通过位运算来判断 n 是否满足上述两个条件: # 1. (n & (n - 1)) == 0: 判断 n 是否为 0 或 2 的幂次方, # 即保证所有二进制...
详见:https://leetcode.com/problems/power-of-four/description/ C++: 方法一: classSolution{public:boolisPowerOfFour(intnum){while(num&&(num%4==0)){num/=4;}returnnum==1;}}; 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 方法二:
Power of Four Given an integer, write a function to determine if it is a power of four. 整除法 复杂度 时间O(1) 空间 O(1) 思路 最简单的解法,不断将原数除以4,一旦无法整除,余数不为0,则说明不是4的幂,如果整除到1,说明是4的幂。
LeetCode *231.Power of Four (Python Solution) 题目描述 Given an integer (signed 32 bits), write a function to check whether it is a power of 4. 给定一个整数(带符号的32位),写一个函数来检查它是否为4的幂。 Example 1: Input: 16 Output: true Example 2: Input: 5 Output: false ...
500+ LeetCode Problems Solved in C++ - A repository with concise C++ solutions to over 500 LeetCode problems, perfect for preparing for coding interviews. - DSA-LeetCode/342-power-of-four/342-power-of-four.cpp at 31baa7cee7e6b0c8d68e06df7fca6238bef4274e