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 的 ...
随机推荐
- 【转】欧拉回路&特殊图下的哈密顿回路题集
转自:http://blog.csdn.net/shahdza/article/details/7779385 欧拉回路[HDU]1878 欧拉回路 判断3018 Ant Trip 一笔画问题1116 ...
- Window 2008 R2 + IIS7.5 + VS2013 错误代码 0x80070002
HTTP 错误 404.0 - Not Found 您要找的资源已被删除.已更名或暂时不可用.详细错误信息模块 IIS Web Core通知 MapRequest Handler处理程序 Static ...
- Unity3D研究院之打开Activity与调用JAVA代码传递参数
原地址:http://www.xuanyusong.com/archives/667 Unity for Android 比较特殊,Unity for IOS 打包是将XCODE工程直接交给开发 ...
- solr4.5部署
一.服务器部署 1.solr自带jetty服务器上部署 cd到solr-4.5.0\example目录下,运行java -jar start.jar即可运行jetty服务器.访问http://loca ...
- Java的synchronized关键字:同步机制总结
JAVA中synchronized关键字能够作为函数的修饰符,也可作为函数内的语句,也就是平时说的同步方法和同步语句块.搞清楚synchronized锁定的是哪个对象,就能帮助我们设计更安全的多线程程 ...
- 暑假集训单切赛第一场 UVA 1737 Mnemonics and Palindromes 3
题意:求由a,b,c三个字母组成的长度为n的字符串,其任意连续的至少长度大于等于2的子字符串都不是回文,问这个字符串有多少种?并字典序输出 如果所有种类的字符串总长度大于100000个字符,就输出TO ...
- POJ 1731
#include<iostream> #include<string> #include<algorithm> using namespace std; int m ...
- 深入浅出ES6(六):解构 Destructuring
作者 Jason Orendorff github主页 https://github.com/jorendorff 什么是解构赋值? 解构赋值允许你使用类似数组或对象字面量的语法将数组和对象的属性 ...
- Test Markdown Editor
Last night, I just saw a cute blogger's homepage. Then I want to write something. But anyway, I use ...
- 224. Basic Calculator
题目: Implement a basic calculator to evaluate a simple expression string. The expression string may c ...