leetcode Power of Two位运算
来源:互联网 发布:代帮上传淘宝宝贝 编辑:程序博客网 时间:2024/06/03 22:50
Given an integer, write a function to determine if it is a power of two.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
可以直接做:
class Solution {public:bool isPowerOfTwo(int n) {if (n < 1) return false;int res;while (n>0){res = n % 2;if (res != 0){if (n == 1)return true;elsereturn false;}n = n / 2;}}};
也可以用位运算:
class Solution {public:bool isPowerOfTwo(int n) {if (n < 1) return false; return !(n&(n-1));}};
0 0
- leetcode Power of Two位运算
- LeetCode Power of Two 位运算
- LeetCode 231 Power of Two (位运算)
- LeetCode 342 Power of Four (位运算)
- [LeetCode] Power of Two
- 【LeetCode】Power of Two
- [Leetcode]Power of Two
- [leetCode] Power of Two
- leetcode --Power of Two
- leetcode: Power of Two
- LeetCode Power of Two
- [LeetCode] Power of Two
- LeetCode Power of Two
- [leetcode] Power of Two
- [Leetcode]Power of Two
- Leetcode: Power of Two
- [LeetCode]Power of Two
- leetcode | Power of Two
- Pro Android学习笔记(一七零):发布应用(3):打开商店、许可和ProGuard
- Qt之模型/视图
- 获取所有应用的资源id和uri
- linux程序设计——用互斥量进行同步(第十二章)
- java GraphicsMagick入门
- leetcode Power of Two位运算
- svn撤销版本修改
- Android Studio 依赖注入框架 butterknife
- OC基础语法-03 面向对象思想
- Android bundle 传值方法
- 初识Hadoop入门介绍
- 最大堆、最小堆、堆排序
- sed + grep 解决 Android docs 打开慢的问题
- Android通过tcpdump抓包