231. Power of Two
来源:互联网 发布:淘宝客推广专区在哪里 编辑:程序博客网 时间:2024/05/29 04:13
题目来源【Leetcode】
Given an integer, write a function to determine if it is a power of two.
判断一个数是否为2的N次方
方法一:取对数
class Solution {public: bool isPowerOfTwo(int n) { double i = log10(n)/log10(2); return (i-(int)i) == 0; }};
方法二:用二进制的特点
class Solution {public: bool isPowerOfTwo(int n) { return n > 0 && !(n&(n-1)); }};
方法三:用最大的2进制数
class Solution {public: bool isPowerOfTwo(int n) { return n > 0 && 4294967296%n == 0; }};
阅读全文
0 0
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- Servlet,报了一上午404和500的报错
- HDU 5919Sequence II (主席树,不同元素个数+第K大)
- keras构建卷积神经网络识别cifar10
- 如何用十条命令在一分钟内检查Linux服务器性能
- mod函数和rem函数的区别
- 231. Power of Two
- JS原生实现自定义滚动条
- kafka配置参数
- linux下PPPoE Server测试环境搭建
- SpringMVC报404的一种可能
- maven新增tomcat插件
- JPA查询出现死循环
- OKHTTP源码分析(二)同步方法以及拦截器链
- pandas 读取csv Initializing from file failed