【easy】power of 2,3,4
============================== 2的幂次 ================================
最佳解法
如果一个数是2的次方数的话,根据上面分析,那么它的二进数必然是最高位为1,其它都为0,那么如果此时我们减1的话,则最高位会降一位,其余为0的位现在都为变为1,那么我们把两数相与,就会得到0,用这个性质也能来解题,而且只需一行代码就可以搞定,如下所示:
class Solution {
public:
bool isPowerOfTwo(int n) {
return (n > ) && (!(n & (n - )));
}
};
递归
public boolean isPowerOfTwo(int n) {
if(n==)
return true;
if(n>= && n%==)
return isPowerOfTwo(n/);
return false;
}
位运算
public bool isPowerOfTwo(int n) {
if(n<=)
return false;
return countBit(n)==;
}
public int countBit(int num){
int count=;
while(num!=){
count += (num & );
num >>= ;
}
return count;
}
======================================= 3的幂次 ====================================
//一个基本的事实就是如果n是3的x次方,那么以3为底对数后一定是一个整数,否则不是
//还有枚举法,枚举所有可能的int范围内的3的幂次
class Solution {
public:
bool isPowerOfThree(int n) {
double res = log10(n) / log10(); //有精度问题,不要用以指数2.718为低的log函数
return (res - int(res) == ) ? true : false;
}
};
题目不建议,但是用迭代是可以解的:如果一个数是3的x次方那么,反复除以3,最终一定等于1,return true
class Solution {
public:
bool isPowerOfThree(int n) {
int num=n;
while(num> && num%==)
num/=;
return num==;
}
};
=================================== 4的幂次 ==========================================
自己的笨办法:如果是4的幂次,那么二进制只有一个1,0的个数为2,4,6,8等偶数
class Solution {
public:
bool isPowerOfFour(int num) {
if (num<) return false;
if (num==) return true;
int count_zero = ;
int count_one = ;
while(num!=){
if ((num & )==)
count_zero ++;
else
count_one ++;
num >>= ;
}
if (count_one != || count_zero <)
return false;
if (count_zero%==)
return true;
else
return false;
}
};
不符合要求的递归写法
class Solution {
public:
bool isPowerOfFour(int num) {
while (num && (num % == )) {
num /= ;
}
return num == ;
}
};
用log的换底公式来做
class Solution {
public:
bool isPowerOfFour(int num) {
return num > && int(log10(num) / log10()) - log10(num) / log10() == ;
}
};
首先根据Power of Two中的解法二,我们知道num & (num - 1)可以用来判断一个数是否为2的次方数,更进一步说,就是二进制表示下,只有最高位是1,那么由于是2的次方数,不一定是4的次方数,比如8,所以我们还要其他的限定条件,我们仔细观察可以发现,4的次方数的最高位的1都是计数位,那么我们只需与上一个数(0x55555555) <==> 1010101010101010101010101010101,如果得到的数还是其本身,则可以肯定其为4的次方数:
class Solution {
public:
bool isPowerOfFour(int num) {
return num > && !(num & (num - )) && (num & 0x55555555) == num;
}
};
或者我们在确定其是2的次方数了之后,发现只要是4的次方数,减1之后可以被3整除:
class Solution {
public:
bool isPowerOfFour(int num) {
return num > && !(num & (num - )) && (num - ) % == ;
}
};
【easy】power of 2,3,4的更多相关文章
- 142. O(1) Check Power of 2【easy】
142. O(1) Check Power of 2[easy] Using O(1) time to check whether an integer n is a power of 2. Have ...
- 【SPOJ】Power Modulo Inverted(拓展BSGS)
[SPOJ]Power Modulo Inverted(拓展BSGS) 题面 洛谷 求最小的\(y\) 满足 \[k\equiv x^y(mod\ z)\] 题解 拓展\(BSGS\)模板题 #inc ...
- 【CF913G】Power Substring 数论+原根
[CF913G]Power Substring 题意:T组询问,每次给定一个数a,让你求一个k,满足$2^k$的10进制的后$min(100,length(k))$位包含a作为它的子串.你只需要输出一 ...
- 170. Two Sum III - Data structure design【easy】
170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...
- 160. Intersection of Two Linked Lists【easy】
160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...
- 206. Reverse Linked List【easy】
206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...
- 203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
- 83. Remove Duplicates from Sorted List【easy】
83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...
- 21. Merge Two Sorted Lists【easy】
21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...
随机推荐
- PHP性能优化:in_array和isset 在大数组查询中耗时相差巨大,以及巧妙使用array_flip
今天在PHP业务开发中,发现了一个问题. 两个较大数组(20万+元素),遍历其中一个$a,另一个数组$b用于查找元素. 比如 foreach($a as $val){ if(in_array($xx, ...
- 看完python这段爬虫代码,java流泪了c#沉默了
哈哈,其实很简单,寥寥几行代码网页爬一部小说,不卖关子,立刻开始. 首先安装所需的包,requests,BeautifulSoup4 控制台执行 pip install requests pip in ...
- 小议SQL数据插入
--数据插入操作:INSERT INTO user_info(username,age) VALUES('ZHANGSAN',20);INSERT INTO user_info(username,ph ...
- 关于gitee代码上传下载
1.在gitee上面创建新分支: 2.复制本地ssh秘钥(C:\Users\Administrator\.ssh) 添加到 gitee设置页面的ssh:(如果之前没有秘钥,就执行ssh-keygen ...
- 《Effective C++》实现:条款26-条款31
条款26:尽可能延后变量定义式的出现时间 C++推荐在使用对象前才定义对象(调用构造函数赋初值) 只在循环中使用的变量定义在循环内部(除非"赋值"成本低于"构造+析构&q ...
- 神经网路-SGD-1
SGD神经网络以及python中实现 1.SGD(stochastic gradient descend):<1>数据抽取:<2>计算梯度;<3>参数更新:< ...
- 【转】Spark实现行列转换pivot和unpivot
背景 做过数据清洗ETL工作的都知道,行列转换是一个常见的数据整理需求.在不同的编程语言中有不同的实现方法,比如SQL中使用case+group,或者Power BI的M语言中用拖放组件实现.今天正好 ...
- css 常见属性
字体属性:(font) 大小 font-size: x-large;(特大) xx-small;(极小) 一般中文用不到,只要用数值就可以,单位:PX.PD 样式 font-style: obliqu ...
- 【坦克大战】Unity3D多人在线游戏(泰课的坦克大战--旋转的螺丝钉)
[坦克大战]Unity3D多人在线游戏 http://www.taikr.com/my/course/937 1.NetworkManager的介绍: 说明:选择固定生成时会自动寻找有StartPos ...
- 关于vue-cli的项目结构【转】
一.总体框架 一个vue-cli的项目结构如下,其中src文件夹是需要掌握的,所以本文也重点讲解其中的文件,至于其他相关文件,了解一下即可. vue-cli项目总体结构 二.文件结构细分 1.buil ...