[leetcode] 231. Power of Two
来源:互联网 发布:淘宝刷皇冠多少钱 编辑:程序博客网 时间:2024/06/15 21:39
Given an integer, write a function to determine if it is a power of two.
Solution 1
Idea: divide n by 2, and check the residual.
class Solution {public: bool isPowerOfTwo(int n) { while (n!=0 && n%2==0){ n = n/2; } return n==1; }};
Solution 2
Idea: 1 -- 1, 2 -- 10, 4 -- 100,... the highest bits are all 1. If minus 1, the left bit become 0 ,and others become 1s. Do bit logic and, should be 0;
class Solution {public: bool isPowerOfTwo(int n) { return (n>0) && ((n&(n-1))==0); }};
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
- 【swift基础】04字符串
- mxnet
- 350. Intersection of Two Arrays II
- Orocos OperationCaller 解析
- java按行读取文件,可做模板(简单易懂),大家一起学习
- [leetcode] 231. Power of Two
- Linux下文件描述符
- 关于csdn上的博客搬家的问题
- 【swift基础】05数组
- 007_Android Studio 2.1.0 导入Android Studio 项目 步骤详解(附详细截图)
- 贝叶斯学习、MAP、ML
- 第一行代码读书笔记——数据存储全方案,持久化技术
- 在面对对象基础姐几个魔术方法
- 开发工具-内容