(easy)LeetCode 231.Power of Two
Given an integer, write a function to determine if it is a power of two.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
代码如下:
public class Solution {
public boolean isPowerOfTwo(int n) {
if(n<=0) return false;
if(n==1) return true;
int mod=n;
while(mod>=2 && mod%2==0){
mod/=2;
}
if(mod>=2 && mod%2!=0)
return false;
else
return true;
}
}
运行结果:

(easy)LeetCode 231.Power of Two的更多相关文章
- [LeetCode] 231 Power of Two && 326 Power of Three && 342 Power of Four
这三道题目都是一个意思,就是判断一个数是否为2/3/4的幂,这几道题里面有通用的方法,也有各自的方法,我会分别讨论讨论. 原题地址:231 Power of Two:https://leetcode. ...
- LN : leetcode 231 Power of Two
lc 231 Power of Two 231 Power of Two Given an integer, write a function to determine if it is a powe ...
- [LeetCode] 231. Power of Two 2的次方数
Given an integer, write a function to determine if it is a power of two. Example 1: Input: 1 Output: ...
- LeetCode 231 Power of Two
Problem: Given an integer, write a function to determine if it is a power of two. Summary: 判断一个数n是不是 ...
- Leetcode 231 Power of Two 数论
同样是判断数是否是2的n次幂,同 Power of three class Solution { public: bool isPowerOfTwo(int n) { ) && ((( ...
- Java [Leetcode 231]Power of Two
题目描述: Given an integer, write a function to determine if it is a power of two. 解题思路: 判断方法主要依据2的N次幂的特 ...
- [LeetCode] 231. Power of Two ☆(是否2 的幂)
描述 Given an integer, write a function to determine if it is a power of two. 给定一个整数,编写一个函数来判断它是否是 2 的 ...
- LeetCode - 231. Power of Two - 判断一个数是否2的n次幂 - 位运算应用实例 - ( C++ )
1.题目:原题链接 Given an integer, write a function to determine if it is a power of two. 给定一个整数,判断该整数是否是2的 ...
- leetcode 231 Power of Two(位运算)
Given an integer, write a function to determine if it is a power of two. 题解:一次一次除2来做的话,效率低.所以使用位运算的方 ...
随机推荐
- 删除文件夹工具类 DeleteFolder.java
package com.util; import java.io.File; /** * 删除文件夹 * @createTime DSC 20, 2010 15:38 * @version 2.0 * ...
- android学习笔记16——对话框
android支持丰富的对话框,常用4中对话框: 1.AlertDialog: 2.ProgressDialog:进度对话框,这个对话框只是对进度条的封装 3.DatePickerDialog:日期选 ...
- c++中的peek函数
c++中 cin.peek()函数 其返回值是一个char型的字符,返回值是指针指向的当前字符, 但是只是观测,指针任停留在当前位置,并不后移.如果要访问的字符是文件结束符,则函数值是EOF(-1); ...
- SQL Server 中LEN函数的问题
LEN('T ') =1 LEN(' T') =2 在数据库中分解字符串时要注意,例如以'^'分隔'X ^ T ',分解时要注意最后的'T '被分解成'T' 可用如下的代码来进行完整的分解 SET A ...
- javascript 异步编程-setTimeout
javascript的执行引擎是单线程的,正常情况下是同步编程的模式,即是程序按照代码的顺序从上到下依次顺序执行.只要有一个任务耗时很长,后面的任务都必须排队等着,会拖延整个程序的执行.常见的浏览器无 ...
- Android流量统计TrafficStats类
对于Android流量统计来说在2.2版中新加入了TrafficStats类可以轻松获取,其实本身TrafficStats类也是读取Linux提供的文件对象系统类型的文本进行解析. android.n ...
- hibernate.cfg.xml讲解
<session-factory> <!-- 配置数据库连接信息 --> <!-- 数据库驱动 --> <property name="connec ...
- 探秘JavaScript中的六个字符
JavaScript 是一个奇怪而有趣的语言,我们可以写一些疯狂却仍然有效的代码.它试图帮助我们把事情转换到基于我们如何对待他们的特定类型. 如果我们添加一个字符串,JavaScript会假定我们希望 ...
- Codeforces Round #364 (Div. 2) Cards
Cards 题意: 给你n个牌,n是偶数,要你把这些牌分给n/2个人,并且让每个人的牌加起来相等. 题解: 这题我做的时候,最先想到的是模拟,之后码了一会,发现有些麻烦,就想别的方法.之后发现只要把它 ...
- CF 486D vailid set 树形DP
As you know, an undirected connected graph with n nodes and n - 1 edges is called a tree. You are gi ...