LeetCode 231. Power of Two
来源:互联网 发布:淘宝发错货的处理规定 编辑:程序博客网 时间:2024/06/14 08:48
Given an integer, write a function to determine if it is a power of two.
解法一:
class Solution {public: bool isPowerOfTwo(int n) { double res = log10(n) / log10(2); if((res - (int)res) == 0) return true; return false; }};
后来遇到Power of four,发现了另一种解法,回来补上。一个数如果是power of two,那么num二进制最高位是,1,其余为0,(num - 1)最高位是0,其余是1,所以(num & num - 1)是0;
解法二:
class Solution {public: bool isPowerOfTwo(int n) { return (n > 0) && !(n & (n - 1)); }};
0 0
- [leetcode] 231.Power of Two
- LeetCode ||231. Power of Two
- [LeetCode]231.Power of Two
- Leetcode 231. Power of Two
- leetcode 231. Power of Two
- 231. Power of Two LeetCode
- [LeetCode]231. Power of Two
- [LeetCode]231. Power of Two
- LeetCode 231. Power of Two
- [leetcode] 231. Power of Two
- leetcode 231. Power of Two
- LeetCode *** 231. Power of Two
- leetcode 231. Power of Two
- LeetCode 231. Power of Two
- Leetcode-231. Power of Two
- #leetcode#231. Power of Two
- 231.[Leetcode]Power of Two
- Leetcode 231. Power of Two
- 懒加载(LAZY)在View层报错的解决方法: OpenSessionInViewFilter
- linux Sybase ASE 查询当前版本和所有数据库
- C语言入门(二十)结构体
- Express Error: No default engine was specified and no extension was provided
- myeclipse加载maven工程提示pom.xml无法解析!
- LeetCode 231. Power of Two
- awk 之 in NR FNR 用法实例
- log4j2 使用详解
- 设计模式之我见(四)
- Android 学习记录(持续更新)
- codeforces 241 E Flights
- 使用 GNU Libtool 创建库
- easyui datagrid 部分参数整理
- hive中order by,sort by, distribute by, cluster by作用以及用法