题目:Power of Two

Given an integer, write a function to determine if it is a power of two.

题意:判断一个数是否是2的k次方数。

思路:

2的次方数使用2进制表示则必然只有最高位是1其他都是0;

这样判断一个数最多需要循环32次就能得出结果。

则程序如下:

bool isPowerOfTwo(int n){
if (!n)return false;
while ((n&0x01) != 0x01){//2的次方则只有最高位是1
n = n >> ;
}
return n == ;
}

但是这样还有循环,如何不使用循环一下子判断出来呢?

思路2:

2的次方使用二进制只有一个1,而是用下面的方法,可以判断一个二进制序列中只有一个1.

n&(n-1);//n-1会将n中从小到大第一个为1的位置变成0,这样就能判断只有一个1的情况。

bool isPowerOfTwo(int n){
//2的幂只有一个1,则使用n&(n-1)来统计1的个数
return n > && !(n & (n - ));
}

题目:Power of Three

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?

题意:判断一个数是否是3的k次幂数。

要求:不能使用递归或循环。

思路:

如果可以使用循环,也很简单每次对3求余,就可以了。

bool isPowerOfThree(int n) {
while (n >= ){
if (n % )return false;
n /= ;
}
return n == ;
}

但是,要求不使用循环或递归。

仔细想想,3的k次幂的数就是,该数的因子只有3,那么使用一个在int范围内最大的3的k次幂的数对给定的数求余,如果为0就说明它是3的k次幂的数。

扩展开来,所有的判断n的k次幂的数都是可以使用这个方法。

bool isPowerOfThree(int n) {
//int中3的最大次幂数1162261467
return n > && !( % n);
}

题目: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的k次幂数。

要求:不能使用递归或循环。

思路:

4的次幂,乍一看好像和2的次幂差不多,能不能用类似的方法来求解呢?

仔细一想肯定发现不能单纯判断一个1来判断4的次幂,为什么呢?因为4的次幂写成2进制,1只会出现奇数的位置上,当然排除1的情况。

那么只需要在判断1是不是在奇数的位置上是不是就可以判断它是不是4的k次幂。

推敲一下发现可行,程序如下:

bool isPowerOfFour(int n){
return n > && !(n & (n - )) && ((n & 0x55555555) == n);//0x55555555保证在奇数的位置上的1被保留
}

思路2:

还有一个思路,4^k - 1 = ?

如果k是偶数,不妨设k = 2;因式分解得到(4 - 1)*(4 + 1);

如果k是奇数,不妨设k = 3;因式分解得到(4 - 1)*(4^2 + 4 + 1);

如果k > 2;不妨设k = 2t,则因式分解得到(4^t - 1)*(4^t + 1)

(4^t - 1)同上面的情况,可以发现总可以分出一个(4 - 1)的因子,所以,4^k - 1 必定会有3的因子。

数学不精,没办法精确证明。

bool isPowerOfFour(int n){
return n > && !(n & (n - )) && !((n - )%);
}

[LeetCode]Power of N的更多相关文章

  1. [LeetCode] Power of Four 判断4的次方数

    Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Gi ...

  2. [LeetCode] Power of Three 判断3的次方数

    Given an integer, write a function to determine if it is a power of three. Follow up:Could you do it ...

  3. [LeetCode] Power of Two 判断2的次方数

    Given an integer, write a function to determine if it is a power of two. Hint: Could you solve it in ...

  4. LeetCode Power of Four

    原题链接在这里:https://leetcode.com/problems/power-of-four/ 题目: Given an integer (signed 32 bits), write a ...

  5. LeetCode Power of Three

    原题链接在这里:https://leetcode.com/problems/power-of-three/ 与Power of Two类似.检查能否被3整除,然后整除,再重复检查结果. Time Co ...

  6. Leetcode Power of Two

    Given an integer, write a function to determine if it is a power of two. 题目意思: 给定一个整数,判断是否是2的幂 解题思路: ...

  7. Leetcode Power of two, three, four

    Given an integer, write a function to determine if it is a power of two. Hint: Could you solve it in ...

  8. leetcode power(x,n)

    class Solution { public: double pow(double x, int n) { double a=1; if(n==0)return 1; if(x==1)return ...

  9. LeetCode——Power of Two

    Description: Given an integer, write a function to determine if it is a power of two. public class S ...

随机推荐

  1. 【记录】SpringBoot 2.X整合Log4j没有输出INFO、DEBUG等日志信息解决方案

    由于批量更新的时候一直无法定位问题出处,就去服务器定位日志,奈何日志一直无法输出,为了能够更好的定位问题,痛定思痛后逐步排查最终解决问题.如有客官看到此处,请不要盲目对号入座,我的项目环境或许与你有区 ...

  2. Yarn上常驻Spark-Streaming程序调优

    对于长时间运行的Spark Streaming作业,一旦提交到YARN群集便需要永久运行,直到有意停止.任何中断都会引起严重的处理延迟,并可能导致数据丢失或重复.YARN和Apache Spark都不 ...

  3. Unity 自定义Inspector面板时的数据持久化问题

    自定义Inspector面板的步骤: Unity内创建自定义的Inspector需要在Asset的任意文件夹下创建一个名字是Editor的文件夹,随后这个文件夹内的cs文件就会被放在vstu生成的Ed ...

  4. Jesus Is Here[递推]2015沈阳online

    题目链接https://nanti.jisuanke.com/t/41175 自从百度之星初赛一上自己做出来一道打表找规律的题之后,这种膨胀的感觉让我近乎丧失理智,今天这道题我死死盯了两三个小时硬是没 ...

  5. Java中时间格式处理,指定N天/小时等之后的时间

    1)根据当前时间,获取具体的时刻的时间 N天前 M小时之前 可用 new Date().getTime() - 24 * 60 * 60 * 1000*N[N天之前]的方法来获取处理时间之后的具体的值 ...

  6. Leetcode之二分法专题-278. 第一个错误的版本(First Bad Version)

    Leetcode之二分法专题-278. 第一个错误的版本(First Bad Version) 你是产品经理,目前正在带领一个团队开发新的产品.不幸的是,你的产品的最新版本没有通过质量检测.由于每个版 ...

  7. 关于简单递归在python3中的实现

    话不多说,奉上代码: #倒计时 def count_down(i): if i <= 0: return else: print(str(i)) count_down(i - 1) #求阶乘 d ...

  8. px和dp(内含大量的像素单位详解)

    1.前言: 读完本文你会学到什么: dp(device pixels) px(css pixels) pt(point) ppi(pixels per inch) dpi(dots per inch) ...

  9. HDU-5977 - Garden of Eden 点分治

    HDU - 5977 题意: 给定一颗树,问树上有多少节点对,节点对间包括了所有K种苹果. 思路: 点分治,对于每个节点记录从根节点到这个节点包含的所有情况,类似状压,因为K<=10.然后处理每 ...

  10. SDU暑期集训排位(4)

    SDU暑期集训排位(4) C. Pick Your Team 题意 有 \(n\) 个人,每个人有能力值,A 和 B 轮流选人,A 先选,B 选人按照一种给出的优先级, A 可以随便选.A 想最大化己 ...