LeetCode 231. Power of Two
来源:互联网 发布:toad for oracle mac 编辑:程序博客网 时间:2024/06/06 05:33
描述
Given an integer, write a function to determine if it is a power of two.
分析
如果是power of two, 则2进制表达中,有且仅有一个1. 可以通过移位来数1的个数, 这里用了一个巧妙的办法, 即判断 N & (N-1) 是否为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
- hdu 1869 最短路裸题
- git 配置多个SSH-Key
- GitLab简介 使用
- [LeetCode]05. Longest Palindromic Substring (动态规划)
- Jenkins-Join Plugin
- LeetCode 231. Power of Two
- Intellij插件开发:MonkeyMaster插件的实现(一)——命令行封装与调用
- GitLab/Git在AndroidStudio上的使用
- Jenkins-Throttle Concurrent Builds
- HDU 1284 钱币兑换问题
- web前端工程师面试--岗位介绍
- JAVA 的wait(), notify()与synchronized同步机制
- [POJ2750] 最大连续和 - 线段树
- python之函数enumerate()