Java [Leetcode 231]Power of Two
题目描述:
Given an integer, write a function to determine if it is a power of two.
解题思路:
判断方法主要依据2的N次幂的特点:仅有首位为1,其余各位都为0.
代码如下:
public class Solution {
public boolean isPowerOfTwo(int n) {
return (n > 0) && ( n & (n - 1)) == 0;
}
}
Java [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. ...
- [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: ...
- 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 ...
- Java for LeetCode 231 Power of Two
public boolean isPowerOfTwo(int n) { if(n<1) return false; while(n!=1){ if(n%2!=0) return false; ...
- 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) { ) && ((( ...
- (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 @ ...
- Java [Leetcode 326]Power of Three
题目描述: Given an integer, write a function to determine if it is a power of three. Follow up:Could you ...
- [LeetCode] 231. Power of Two ☆(是否2 的幂)
描述 Given an integer, write a function to determine if it is a power of two. 给定一个整数,编写一个函数来判断它是否是 2 的 ...
随机推荐
- Ajax出入江湖
window.onload = initAll; var xhr = false; function initAll() { if (window.XMLHttpRequest) { xhr = ne ...
- spoj 147
dfs枚举真值 #include <cstdio> #include <cstring> #include <cstdlib> #include <stack ...
- android 解析XML方式(二)
上一节中,我们使用DOM方式解析xml文档,该方式比较符合我们日常思维方式,容易上手,但是它直接把文档调入内存中,比较耗内存.在这里我们可以用另外一种方式解析xml,这个就是SAX方式. SAX即是: ...
- windowopen
1.最基本的弹出窗口代码] <SCRIPT LANGUAGE="javascript"> <!-- window.open ('page.html') --> ...
- unity与Android相互调用
原地址:http://www.cnblogs.com/ayanmw/p/3727782.html 现在unity 导出的android客户端需要调用 Android 的支付SDK,但是unity与an ...
- VS2010 MFC DataGrid绑定实例
VS2010环境下MFC使用DataGrid绑定数据源 参考:http://blog.csdn.net/fddqfddq/article/details/7874706 详细介绍如何在MFC中使用Da ...
- JDK与JRE
dos命令行中常见的命令: 1.dir:列出当前目录下的文件以及文件夹 2.md:创建目录(即文件夹) |-----C:\>md kkk(在C盘下创建了一个名为kkk的文件夹) 3.rd:删除目 ...
- activiti5.13 框架 数据库设计说明书
转载自:http://www.cnblogs.com/llzgzljl/p/3356108.html activiti5.13 框架 数据库设计说明书 1.结构设计 1.1. 逻辑结构设计 Ac ...
- 57. Insert Interval
题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...
- Intellij IDEA 构建Spring Web项目 — 用户登录功能
相关软件: 1.Intellij IDEA14:http://pan.baidu.com/s/1nu16VyD 2.JDK7:http://pan.baidu.com/s/1dEstJ5f 3.Tom ...