【Leetcode】Power of Two
来源:互联网 发布:linux系统root密码 编辑:程序博客网 时间:2024/05/24 03:18
题目链接:https://leetcode.com/problems/power-of-two/
题目:
Given an integer, write a function to determine if it is a power of two.
思路:
如果小于0,则不是2的n次幂。
否则则转为二进制,只有最高位为1,剩余为0的时候才是2的n次幂。
算法:
public boolean isPowerOfTwo(int n) {if (n < 0)return false;String binary = Integer.toBinaryString(n);char c[] = binary.toCharArray();if (c[0] == '1') {for (int i = 1; i < c.length; i++) {if (c[i] != '0')return false;}} else {return false;}return true;}
0 0
- [LeetCode] Power of Two
- 【LeetCode】Power of Two
- [Leetcode]Power of Two
- [leetCode] Power of Two
- leetcode --Power of Two
- leetcode: Power of Two
- LeetCode Power of Two
- [LeetCode] Power of Two
- LeetCode Power of Two
- [leetcode] Power of Two
- [Leetcode]Power of Two
- Leetcode: Power of Two
- [LeetCode]Power of Two
- leetcode | Power of Two
- 【leetcode】Power of Two
- [LeetCode]Power of Two
- [leetcode] Power of two
- LeetCode Power of Two
- 用EA(Enterprise Architect)将C++的".h"文件转换成UML类图
- Hive_4. DML -- 数据转换
- 网络框架AndNet—解放你的时间
- iOS-iOS8之后,push界面导航栏上方空出20像素
- AndroidStudio使用SVN
- 【Leetcode】Power of Two
- C语言位域(位段)
- ios mac ox10.11.1安装cocoapods时NoMethodError问题
- 手机防盗模块的实现
- Lua游戏开发(三)---Lua语言学习(上)
- hdu1226超级密码【数字广搜+大数取模】
- TTL 电平 CMOS电平 RS232 电平
- 从getmemery()函数看内存管理、函数传参等一系列问题
- openstack ssh卡死expecting SSH2_MSG_KEX_DH_GEX_GROUP