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 的 ...
随机推荐
- POJ 3321 Apple Tree(后根遍历将树转化成序列,用树状数组维护)
题意:一棵树,有很多分叉,每个分叉上最多有1个苹果. 给出n,接下来n-1行,每行u,v,表示分叉u,v之间有树枝相连.这里数据中u相当于树中的父节点,v相当于子节点. 给出两个操作: 1.C x ...
- java核心技术记录之集合
java库中的具体集合: 集合类型 描述 ArrayList 一种可以动态增长和缩减的索引序列 LinkedList 一种可以在任何位置进行高效地插入和删除操作的有序序列 ArrarDeque 一种用 ...
- Win7-其中的文件夹或文件已在另一个程序中打开
Win7-其中的文件夹或文件已在另一个程序中打开 如何解决Win7系统在删除或移动文件时提示,“操作无法完成,因为其中的文件夹或文件已在另一个程序中打开,请关闭该文件夹或文件,然后重试”. 步骤阅 ...
- 在win7之后安装Ubuntu14.04系统后,丢失对win7的引导
安装完ubuntu 14.04后,开机直接进入ubuntu.但是win7下的分区还在,只是未显示引导菜单. 恢复步骤: (前提是机器上确实存在win7的引导) $ sudo update-grub G ...
- 【nginx网站性能优化篇(2)】反向代理实现Apache与Nginx的动静分离(LNMPA)
为什么要使用反向代理 具体请参考这篇博文:[Linux常识篇(1)]所谓的正向代理与反向代理 在虚拟机上配置反向代理的步骤 首先假设你已经假设好了LNMP架构了,这时我们还要安装Apache和php, ...
- iOS开发--网络下载
这里使用的是NSURLConnection的代理请求下载,并且是具有进度,UI能实时刷新,至于NSURLConnection如何请求.并且有几种请求方法请看NSURLConnection请求简介,在这 ...
- C#基础练习(事件登陆案例)
Form1的后台代码: namespace _08事件登陆案例 { public partial class Form1 : Form { public Form1() ...
- Spring使用外部的配置文件
在使用Spring做web项目的时候,通常会使用到数据库的连接信息 jdbcUrl driverClass username password 那么应该如何使用这些属性呢? 如在Spring中使用数据 ...
- Android ActionBar中的下拉菜单
在ActionBar中添加下拉菜单,主要有一下几个关键步骤: 1. 生成一个SpinnerAdapter,设置ActionBar的下拉菜单的菜单项 2. 实现ActionBar.OnNavigatio ...
- smartcomb:用php实现的web模块拼合器
smartcomb是一个用php实现的web模块拼合器,相对于其他的代码拼合工具,如下特性: 可以拼合任意类型的文件,不限于js文件. 集中并声明依赖,自动分析依赖拼合,按需加载. 支持多种配置切换 ...