题目: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. Collectors.toMap不允许Null Value导致NPE

    背景 线上某任务出现报警,报错日志如下: java.lang.NullPointerException: null at java.util.HashMap.merge(HashMap.java:12 ...

  2. lombok 下的@Builder注解用法

    pom依赖 <dependency> <groupId>org.projectlombok</groupId>            <artifactId& ...

  3. C#数据结构_排序

    /** * 冒泡排序 * * @param array * @return */ public static int[] bubbleSort(int[] array) { if (array.len ...

  4. ACM-数论-广义欧拉降幂

    https://www.cnblogs.com/31415926535x/p/11447033.html 曾今一时的懒,造就今日的泪 记得半年前去武大参加的省赛,当时的A题就是一个广义欧拉降幂的板子题 ...

  5. Elasticsearch核心技术(2)--- 基本概念(Index、Type、Document、集群、节点、分片及副本、倒排索引)

    Elasticsearch核心技术(2)--- 基本概念 这篇博客讲到基本概念包括: Index.Type.Document.集群,节点,分片及副本,倒排索引. 一.Index.Type.Docume ...

  6. SpringMVC中的generator

    引言 今天在做一个原生的spring项目的时候碰到一个非常好用的代码自动生成器,叫做generator,主要是运用于mybatis中的代码生成,它可以生成mapper的映射xml,model中的实体类 ...

  7. Docker详解(一)

    目录 Docker简介 Docker组成 永远的HelloWorld 序言:众所周知,近几年的互联网各项技术发展的如火如荼,敏捷开发模式越来越普及,"快"似乎成为了行业的标准,于是 ...

  8. 工作中遇到的99%SQL优化,这里都能给你解决方案(二)

    -- 示例表 CREATE TABLE `employees` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(24) NOT NULL ...

  9. 【LeetCode】62-不同路径

    题目描述 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为& ...

  10. 使用openlivewriter编写cnblogs博客

    下载OpenLiveWriter 下载地址:http://openlivewriter.org/ 安装OpenLiveWriter 1.账号配置 2.常规操作,省略- 安装高亮插件 1.下载插件:ht ...