leetcode:231 Power of Two-每日编程第十八题
来源:互联网 发布:mac keynote 模板 编辑:程序博客网 时间:2024/06/05 15:07
Power of Two
Total Accepted: 42645 Total Submissions: 128399 Difficulty: Easy
Given an integer, write a function to determine if it is a power of two.
思路:
1).0可不是2的阶乘,因此,要在函数头判断输入是否为0。
2).如果n是2的阶乘,那么n用二进制表示,就是1个1,后面跟着一串0。因此,只要把末尾所有0全部移除,再判断其是否为1,就得出结果了。
class Solution {public: bool isPowerOfTwo(int n) { if(n==0){ return false; } while((n&1)==0){ n>>=1; } if(n==1){ return true; }else{ return false; } }};
0 0
- leetcode:231 Power of Two-每日编程第十八题
- LeetCode每日一题——231. Power of Two
- leetcode 每日一题 231. Power of Two 326. Power of Three
- 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
- Power of Two(leetcode 231)
- 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
- 10.DNS服务器搭建
- How to deal with the error message "Could not load SAPGUIResources"
- 线程同步——信号量
- 操作系统---基础题目汇总十一
- AppleStore下载量查看
- leetcode:231 Power of Two-每日编程第十八题
- UITextField判断用户输入是否为邮箱
- elasticsearch 离线安装head,bigdesk插件
- 怎样正确做 Web 应用的压力测试
- 【android_温故知新】各种杂项组件
- 无法启动此程序,因为计算机中丢失VCRUNTIME140.dll 尝试重新安装此程序以解决此问题
- 11.iptables防火墙设置
- 网络存储IP SAN与IB SAN
- Android Studio:查看SHA1及MD5