leetcode 231. Power of Two
来源:互联网 发布:校名关键词优化方法 编辑:程序博客网 时间:2024/05/09 11:27
原题:
Given an integer, write a function to determine if it is a power of two.
题意:
给你一个数,问你这个数是不是2的n次方。
class Solution {public: bool isPowerOfTwo(int n) { if(n==1) return true; if(n%2) return false; while(n) { n=n/2; if(n==1) return true; if(n%2) return false; } }};
解答:
我的想法很粗暴,就是不断的除以2判断除以2以后是否还是一个偶数,除到最后是1.那么答案就正确。
我看到了一个别人的解法,觉得很好~真是简单
bool isPowerOfTwo(int n) { return (n > 0) && (!(2147483648%n)); //2147483648 = 2^31}
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
- CentOs 基础知识
- opencv各个版本更新内容(持续更新ing)
- 第一课:打开一个窗口
- 你的Eclipse是否能顺利进入调试 ?
- swift手记-6
- leetcode 231. Power of Two
- 九度OJ 题目1437:To Fill or Not to Fill
- XML——使用 XPath来定位信息+使用命名空间
- Python中的正则表达式
- hdu 1233 还是畅想工程(prim算法)
- linux性能调试----cpu篇
- win 10系统安装myeclipse 9.1 破解问题
- 自动适配之Masonry适配
- Android第一天