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?

 1 /*************************************************************************
 2     > File Name: LeetCode342.c
 3     > Author: Juntaran
 4     > Mail: [email protected]
 5     > Created Time: 2016年05月10日 星期二 02时50分00秒
 6  ************************************************************************/
 7
 8 /*************************************************************************
 9
10     Power of Four
11
12     Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
13
14     Example:
15     Given num = 16, return true. Given num = 5, return false.
16
17     Follow up: Could you solve it without loops/recursion?
18
19  ************************************************************************/
20
21 #include "stdio.h"
22
23 int isPowerOfFour(int num) {
24     double tmp = log10(num)/log10(4);
25     return tmp == (int)tmp ? 1 : 0;
26 }
27
28 int main()
29 {
30     int n = 9;
31     int ret = isPowerOfFour(n);
32     printf("%d\n",ret);
33
34     n = 16;
35     ret = isPowerOfFour(n);
36     printf("%d\n",ret);
37
38     return 0;
39 }
时间: 2024-11-20 11:21:38

LeetCode 342的相关文章

leetCode 342. Power of Four 位运算

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? 题目大意: 判断一个数是否为4的幂. 思路

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? 先说明用循环: 如下 class Solution { public: bool

Python [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. 解题思路: 位操作. 首先判断是不是只有一位数字为1,其余为0 然后判断为1的位置是不是奇数位 代码如下: class Solution(object): def isPower

LeetCode 342. Power of Four (4的次方)

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? 题目标签:Bit Manipulation 这道题目让我们判断一个数字是不是4的

[LeetCode] 342. Power of Four(位操作)

传送门 Description 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? 思路 题意:不使用循环和递归,求一个数是否是4的

Leetcode 342 Power of Four 数论

题意:判断一个数是不是4的幂数,和Power of two类似. 先判断num是否大于0,再判断num是否能开根号,最后判断num开根号后的数是否是2^15的约数. 提示:4的幂数开根号就是2的幂数. 注意:判断一个双精度数a是否是0,请用fabs(a)<一个极小的数,如本题中用的是1e-9. 1 class Solution { 2 public: 3 bool isPowerOfFour(int num) { 4 return (num > 0 ) && (fabs((in

[LeetCode] 342. 4的幂 ☆(是否4 的幂)

描述 给定一个整数 (32 位有符号整数),请编写一个函数来判断它是否是 4 的幂次方. 示例 1: 输入: 16输出: true示例 2: 输入: 5输出: false 进阶:你能不使用循环或者递归来完成本题吗? 解析 32位数如果是4的幂,那么只有奇数位有且只有一个1,偶数位都是0.判断条件为: 1. 与0xaaaaaaaa做与运算结果为0.(a=1010) 2. num & (num - 1) == 0,说明只有1位的  1 代码 public boolean isPowerOfFour(

LeetCode 第 342 题(Power of Four)

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) 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? 题目非常eas