1. 问题

231. Power of Two: 判断一个整数是否是2的n次方,其中n是非负整数

342. Power of Four: 判断一个整数是否是4的n次方,其中n是非负整数

326. Power of Three: 判断一个整数是否是3的n次方,其中n是非负整数

2. 思路

1)2的n次方 

不妨列举几个满足条件的例子。

If n = 0: 2 ^ n = 1

If n = 1: 2 ^ n = 2 -> 10(二进制表示)

If n = 2: 2 ^ n = 4 -> 100(二进制表示)

If n =3: 2 ^ n = 8 -> 1000 (二进制表示)

...

If n = k, 2^n -> 100 .(k个0).. 000 (二进制表示)

可以看到,所有的2的n次方数,都以1开头,然后跟随了n个0。因此,对应的2 ^ n - 1是:

n=1: 2^1 - 1 = 0

If n = 1: 2 ^ n - 1 = 1 -> (0)1(二进制表示)

If n = 2: 2 ^ n - 1 = 3 -> (0)11(二进制表示)

If n =3: 2 ^ n - 1 = 7 -> (0)111 (二进制表示)

...

If n = k, 2^n-1 -> (0)11 .(k个1).. 111 (二进制表示)

最终代码只需要一行就可以解决:

class Solution {
public:
bool isPowerOfTwo(int num) {
return !(num&(num-1)) && n >= 1;
}
};

  

2)4的n次方

  • 解法1:

我觉得这道题跟2的n次方是有点像的。唯一的不同是需要解决属于2的n次方但不属于4的n次方的部分。

$A = \{x \mid 2^n = x \ for \ some \ n \in \ \mathbb{N} \}$

$B = \{x \mid \exists n \in \ \mathbb{N}, \forall m \in \mathbb{N} \Rightarrow 2^n = x \ and \ 4^m \ne x  \}$

先观察属于2的n次方但不属于4的n次方的部分。

2 -> 10

8 -> 1000

32 -> 100000

128 -> 10000000

再观察属于4的n次方的部分。

1 -> 1

4 -> 10

16 -> 10000

64 -> 1000000

可以看到,他们唯一的不同就是从右往左数的第2,4,6,8, ... , 2m位上有没有1。

代码:

class Solution {
public:
bool isPowerOfFour(int n) {
return (!(n&(n-1))) && n >= 1 && n==(n&0x55555555);
}
};
  • 解法(Solution) 2:

简单来说,所有4的n次方减去1都可以被3整除,但所有属于2的次n方而不属于4的n次方的数减去一都不可以被3整除。部分证明(需要用到离散数学)如下:

证明1:对任意 $x \in C=A-B$ 有 (x - 1) % 3 = 0.

$4^n=(1+3)^n = C^0_n 1+C^1_n3+...+C^n_n 3^n = 1+3(C^1_n + C^2_n 3 +...+ C^n_n 3^{n-1})$

$(C^1_n + C^2_n 3 +...+ C^n_n 3^{n-1}) \in \mathbb{Z} $

$\therefore (4^n -1)\ (mod\ 3) ≡ 0$

证明2:对任意$ y \in A$, 有 (x - 1) % 3 =1.

定理:$ if \ a_1 ≡ b_1( mod\ m),\ a_2 ≡ b_2( mod\ m) ,\ then \ a_1 * a_2 ≡ b_1 * b_2(mod m)$

$ 2^2 (mod\ 3)≡ 1,\ 2^1(mod\ 3)≡2$

$\therefore 2^3(mod\ 3) ≡ 2^1 * 2^2 (mod\ 3)≡ 2*1 ≡ 2$

$\therefore 2^5(mod\ 3) ≡ 2^3 * 2^2 (mod\ 3)≡ 2*1≡ 2 \\ ... \\ \therefore 2^{2k+1} (mod \ 3) ≡ (2^{2k-1} (mod \  3))*(2^2 (mod\ 3)) ≡ 2 \ne 0$

定理:$ if\ a_1 ≡ b_1( mod\ m),\ a_2 ≡ b_2( mod\ m) ,\ then \ a_1 - a_2 ≡ b_1 - b_2(mod\ m)$

$\therefore 2^{2k+1} -1(mod\ 3) ≡ 1\ne 0$

代码Code:

class Solution {
public:
bool isPowerOfFour(int n) {
return (!(n&(n-1))) && n >= 1 && (n-1)%3 == 0;
}
};

  

3)3的n次方

简单来说,如果一个数可以整除3的k次方,那么这个数必然也是3的n次方数(其中n<k)。学过离散数学的可以联想到:

已知对于任意的一个正整数N,有$N=p_1^{e1}p^{e2}_2...p^{er}_r$ 且$p_1、p_2...p_r$都为素数。而3是一个素数。因此:
$\forall n, m\in \mathbb{N}$
$n<m\ and \ \exists k\in \mathbb{N}, such\ that\ m=3^k$
$if\ m(mod\ n)≡0,\ then\ there\ must\ be \ n = 3^l \ for \ some \ l\in \mathbb{N}$

代码如下:

class Solution {
public:
bool isPowerOfThree(int n) {
return n > 0 and int(pow(3,19)) % n == 0;
}
};

 

类似的,因为2也是个素数,那么对于2的n次方也可以用相似的方法求解:

class Solution {
public:
bool isPowerOfTwo(int n) {
return n > 0 && (int(pow(2,30))) % n == 0; }
};

  

LeetCode - 326, 342, 231 Power of Three, Four, and Two的更多相关文章

  1. LeetCode 第 342 题(Power of Four)

    LeetCode 第 342 题(Power of Four) Given an integer (signed 32 bits), write a function to check whether ...

  2. 【leetcode❤python】231. Power of Two

    #-*- coding: UTF-8 -*- class Solution(object):    def isPowerOfTwo(self, n):        if(n<=0):     ...

  3. [LeetCode] 231 Power of Two && 326 Power of Three && 342 Power of Four

    这三道题目都是一个意思,就是判断一个数是否为2/3/4的幂,这几道题里面有通用的方法,也有各自的方法,我会分别讨论讨论. 原题地址:231 Power of Two:https://leetcode. ...

  4. 【一天一道LeetCode】#342. Power of Four

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  5. 231. Power of Two 342. Power of Four -- 判断是否为2、4的整数次幂

    231. Power of Two Given an integer, write a function to determine if it is a power of two. class Sol ...

  6. leetcode 326. Power of Three(不用循环或递归)

    leetcode 326. Power of Three(不用循环或递归) Given an integer, write a function to determine if it is a pow ...

  7. LeetCode 第 342 题(Power of Four)

    LeetCode 第 342 题(Power of Four) Given an integer (signed 32 bits), write a function to check whether ...

  8. [LeetCode] 231. Power of Two 2的次方数

    Given an integer, write a function to determine if it is a power of two. Example 1: Input: 1 Output: ...

  9. Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

    1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...

随机推荐

  1. DQL-常见的函数

    一.概述功能:类似于java中的方法好处:提高重用性和隐藏实现细节调用:select 函数名(实参列表); 二:常用的函数: ① 单行函数 1.分组函数 1.sum(),avg(),max(),min ...

  2. Context initialization failed org.springframework.beans.factory.BeanCreationException

    严重: Context initialization failed org.springframework.beans.factory.BeanCreationException: Error cre ...

  3. Oracle作业3 —— 简单查询

    一.创建学生表 CREATE TABLE STUDENTS( STU_ID ) CONSTRAINTS PK_STUS_ID PRIMARY KEY, STU_NAME ) NOT NULL, STU ...

  4. Java并发编程(一)为什么要并发

    并发所带来的好处 1. 并发在某些情况(并不是所有情况)下可以带来性能上的提升 1) 提升对CPU的使用效率 提升多核CPU的利用率:一般来说一台主机上的会有多个CPU核心,我们可以创建多个线程,理论 ...

  5. webpack之理解loader

    我们在写webpack配置文件的时候,应该有注意到经常用到loader这个配置项,那么loader是用来做什么的呢? loader其实是用来将源文件经过转化处理之后再输出新文件. 如果是数组形式的话, ...

  6. 自己花了2天时间,重新整理了个全面的vue2的模板

    自己花了2天时间,重新整理了个全面的vue2的模板,基本vue中需要的部分都整理封装好了,希望大家喜欢^ ^.欢迎大家star或者fork呀~,https://github.com/qianxiaon ...

  7. 了解jQuery并掌握jQuery对象和DOM对象的区别

    jQuery的优势: 开源--开放源代码 轻量级 强大的选择器 出色的DOM操作(对DOM元素的一个增删改查) 完善的Ajax,出色的浏览器兼容性,丰富的插件支持,完善的文档(说明书) 链式操作方式, ...

  8. 10分钟搞定webpack打包

    入门前端这个职位近三年的时间了,但是脑子里的东西不多也不少,今天就从脑袋里把新版本的webpack打包过程拔出来给大家鲁一遍,就算帮助那些小白了,废话不多说,开始鲁起来,大家跟着我一起撸... 首先, ...

  9. [Doctrine Migrations] 数据库迁移组件的深入解析三:自定义数据字段类型

    自定义type 根据官方文档,新建TinyIntType类,集成Type,并重写getName,getSqlDeclaration,convertToPHPValue,getBindingType等方 ...

  10. SVN错误记录

    1.SVN错误:Attempted to lock an already-locked dir 发生这个错误多是中断提交导致了,执行clear后可修复 右键项目--->team--->清理 ...