231. Power of Two
来源:互联网 发布:100m网络下载速度 编辑:程序博客网 时间:2024/05/01 18:54
Given an integer, write a function to determine if it is a power of two.
题意:判断一个数是否为2的幂。
思路:2的幂的二进制表示里只有一个1,所以通过检测n的二进制表示里1的个数即可。注意:输入的n可能为负数。
class Solution {public:bool isPowerOfTwo(int n) {int count = 0;while (n > 0){if (n&1 == 1)count++;n = n >> 1;}if (count == 1)return true;return false;}};
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
- Struts-default.xml中拦截器个数
- iOS TableView 自带可拖动重排功能
- Confluence5.1 最新版的安装&破解&汉化
- java基础知识
- 多线程编程基础知识
- 231. Power of Two
- 详细解释强力的图片加载框架 Glide的配置(顺便补充下CollapsingToolbarLayout的一些功能)
- Qt构建OpenCV3.1CMake报错Failed to download opencv_ffmpeg.dll
- 67. Add Binary
- public static void main(String args[]){ }中的String args[ ]
- iOS Core Data简单演练
- 学习linux步骤
- PDF如何转为SWF
- APNS推送以及app内部消息处理、本地通知的使用