[LeetCode]Power of N
题目: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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- LeetCode Power of Four
原题链接在这里:https://leetcode.com/problems/power-of-four/ 题目: Given an integer (signed 32 bits), write a ...
- LeetCode Power of Three
原题链接在这里:https://leetcode.com/problems/power-of-three/ 与Power of Two类似.检查能否被3整除,然后整除,再重复检查结果. Time Co ...
- Leetcode Power of Two
Given an integer, write a function to determine if it is a power of two. 题目意思: 给定一个整数,判断是否是2的幂 解题思路: ...
- 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 ...
- 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 ...
- LeetCode——Power of Two
Description: Given an integer, write a function to determine if it is a power of two. public class S ...
随机推荐
- CSS布局:元素垂直居中
CSS布局之元素垂直居中 本文将依次介绍在不同条件下实现垂直居中的多种方法及简单原理 Tip:下文中说的适用场景只是举了几个简单的例子方便读者理解.实际应用场景太复杂,生搬硬套容易出错.最重要的是掌握 ...
- 打包一沓开源的 C/C++ 包管理工具送给你!
本文作者:HelloGitHub-ChungZH 博客地址:https://chungzh.cn/ 包管理器可以帮助你更方便地安装依赖关系,并决定所安装的版本,提高你的开发幸福感.许多语言都有自己的包 ...
- 服务注册发现、配置中心集一体的 Spring Cloud Consul
前面讲了 Eureka 和 Spring Cloud Config,今天介绍一个全能选手 「Consul」.它是 HashiCorp 公司推出,用于提供服务发现和服务配置的工具.用 go 语言开发,具 ...
- python 18 re模块
目录 re 模块 1. 正则表达式 2. 匹配模式 3. 常用方法 re 模块 1. 正则表达式 \w 匹配字母(包含中文)或数字或下划线 \W 匹配非字母(包含中文)或数字或下划线 \s 匹配任意的 ...
- CSS3 translate导致字体模糊
今日客户反馈,发现 使用了 translate会导致字体模糊. .media-body-box{ @media all and (min-width: 992px){ position: absolu ...
- Java内部类使用注意事项
Java内部类使用注意事项: 1. 非静态内部类成员可以访问外部类实例成员 (如注释1),但外部类访问非静态内部类的成员 必须创建非静态内部类对象来访问其成员,如注释2 public class La ...
- C# 生产者与消费者模式
情景:一个线程不断获取数据,另一个线程不断处理这些数据. 常规方法:数据列表加锁,两个线程获取锁,拿到操作权:类似代码如下:(不推荐) static void Main(string[] args) ...
- python控制窗口显示隐藏
import win32con # 定义 import win32gui # 界面 import time # 时间 QQ= win32gui.FindWindow("TXGuiFounda ...
- HDU-6437 Videos
HDU-6437 题意:一天有n个小时,现在有m场电影,每场电影有一个愉悦值,有k个人,电影分2种类型A, B, 并且每一场电影只能一个人看, 一个人可以看无数次电影, 只要时间足够, 但是连续看同一 ...
- CF - 652 E Pursuit For Artifacts 边双联通
题目传送门 题解总结起来其实很简单. 把所有的边双联通分量缩成一个点,然后建立好新边, 然后再从起点搜到终点就好了. 代码: /* code by: zstu wxk time: 2019/02/23 ...