leetcode342——Power of Four(C++)

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.

个人博客:http://www.cnblogs.com/wdfwolf3/

这道题本身没有难度,这里只是介绍两种思路,当我们判断出它二进制只有1个1的时候,即必为2的幂时,如何进一步判断它是不是4的幂。

1.  8ms

class Solution {
public:
    bool isPowerOfFour(int num) {
        if(num<=0)
            return false;
        if((num&(num-1))!=0)          //判断是不是2的幂,或者说二进制是否只有1个1
            return false;
        if((num-1)%3==0)        //判断这个1的位置,来判断是不是4的幂
            return true;
        return false;
    }
};

为什么要利用num-1后能不能整除3判断,很多人各种数学证明,其实从二进制角度很好理解证明。num减1后得到的数字末尾全为1,3的二进制是……11,那么从最低位算起有偶数个1的数字都能整除3,奇数个不能整除。自然4的幂减1后为偶数个1。

2.  8ms

class Solution {
public:
    bool isPowerOfFour(int num) {
        if(num<=0)
            return false;
        if((num&(num-1))!=0)
            return false;
        int con=0x55555555;
        if((num&con)!=0)
            return true;
        return false;
    }
};

这里利用了一个数字0x55555555,它是01010101……01010101。4的幂二进制中的1的位置一定出现在0x55555555二进制1的位置,那么按位与操作后等于0说明位置不对,那就不是4的幂。

时间: 2024-08-30 08:32:41

leetcode342——Power of Four(C++)的相关文章

poj2109 Power of Cryptography(数学题)

题目链接:http://poj.org/problem?id=2109 Description Current work in cryptography involves (among other things) large prime numbers and computing powers of numbers among these primes. Work in this area has resulted in the practical use of results from num

326. Power of Three(LeetCode)

Given an integer, write a function to determine if it is a power of three. Follow up:Could you do it without using any loop / recursion? 1 class Solution { 2 public: 3 bool isPowerOfThree(int n) { 4 if (n == 0) 5 return false; 6 while (n != 1) 7 { 8

342. Power of Four(LeetCode)

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 class Solution { 2 public: 3 bool isPo

[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的

342. Power of Four(One-line)

342. Power of Four Total Accepted: 707 Total Submissions: 2005 Difficulty: Easy 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: Cou

HDU 6623 Minimal Power of Prime(思维)题解

题意: 已知任意大于\(1\)的整数\(a = p_1^{q_1}p_2^{q_2} \cdots p_k^{q_k}\),现给出\(a \in [2,1e18]\),求\(min\{q_i\},q \in [1, k]\).即求质因数分解后,最小指数是多少. 思路: 因为\(a \in [2,1e18]\),所以我们现打一个\(1e4\)以内的质数表,然后直接求出\(1e4\)以内的情况. 上面弄完了,那么现在最多只有\(4\)个质因子,情况如下: \(n = p^4\),这种情况就是\(4\

HDU 6623&quot;Minimal Power of Prime&quot;(数学)

传送门 •题意 给你一个大于 1 的正整数 n: 它可以分解成不同的质因子的幂的乘积的形式,问这些质因子的幂中,最小的幂是多少. •题解 定义 $ans$ 表示最终答案: ①如果 $ans \ge 5$: 那么,肯定有 $n=p^{ans}\ ,\ p \le \sqrt[{ans}]{n}$,也就是说 $\ p \le 10^{\frac{18}{5}}$: 所以,我们可以提前预处理出 $[1,10000]$ 的素数表 •Code 1 #include<bits/stdc++.h> 2 us

linux驱动程序之电源管理 之linux休眠与唤醒(2)

在Linux中,休眠主要分三个主要的步骤:(1)冻结用户态进程和内核态任务:(2)调用注册的设备的suspend的回调函数:(3)按照注册顺序休眠核心设备和使CPU进入休眠态.       冻结进程是内核把进程列表中所有的进程的状态都设置为停止,并且保存下所有进程的上下文.当这些进程被解冻的时候,他们是不知道自己被冻结过的,只是简单的继续执行.如何让Linux进入休眠呢?用户可以通过读写sys文件/sys /power/state 是实现控制系统进入休眠.比如: # echo standby >

LeetCode191 Number of 1 Bits. LeetCode231 Power of Two. LeetCode342 Power of Four

位运算相关 三道题 231. Power of Two Given an integer, write a function to determine if it is a power of two. (Easy) 分析: 数字相关题有的可以考虑用位运算,例如&可以作为筛选器. 比如n & (n - 1) 可以将n的最低位1删除,所以判断n是否为2的幂,即判断(n & (n - 1) == 0) 代码: 1 class Solution { 2 public: 3 bool isP