LeetCode 231. Power of Two(2的N次幂)
来源:互联网 发布:北京致远软件 编辑:程序博客网 时间:2024/06/05 03:34
原题网址:https://leetcode.com/problems/power-of-two/
Given an integer, write a function to determine if it is a power of two.
思路:数字n是2的整数次幂,当且仅当n的二进制表示有且只有一个1。
方法一:直接统计1比特数。
public class Solution { public boolean isPowerOfTwo(int n) { int count = 0; for(;n>0 && count<=1; count += (n&1), n>>=1); return count == 1; }}
方法二:直接获取最低位1比特。
public class Solution { public boolean isPowerOfTwo(int n) { return n>0 && n == (n & -n); }}
0 0
- LeetCode 231. Power of Two(2的N次幂)
- [LeetCode-231] Power of Two(判断一个数是不是2的若干次幂)
- LeetCode 231. Power of Two ---- 2的幂
- leetcode 231. Power of Two(2的次方)
- LeetCode 231 Power of Two(2的幂)
- LeetCode 326. Power of Three(3的n次幂)
- LeetCode 342. Power of Four(4的n次幂)
- LeetCode OJ 之 Power of Two (2的n次方)
- 231.Power of Two(2的N次方)
- 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
- 231. Power of Two LeetCode
- [LeetCode]231. Power of Two
- [LeetCode]231. Power of Two
- Linux应用服务器搭建手册——JDK安装与配置
- Fresco-Facebook的图片加载框架的使用
- 利用ActivityLifecycleCallBack监控app前后台状态切换,实现手势密码
- window.location.hash属性介绍
- 用Gson包进行解析本地的json文件
- LeetCode 231. Power of Two(2的N次幂)
- 轮廓、边缘、边界的相关函数
- 笔记--PageHelper分页
- Ajax应用详解
- iOS 如何设置启动引导图
- 循环队列学习
- tftp,ftp服务搭建
- 单例的获取and序列化和反序列化破坏单例的原因及解决方法
- 最小生成树(Minimum Spanning Tree)(Kruskal算法)