Java [Leetcode 231]Power of Two
2016-01-05 14:48
351 查看
题目描述:
Given an integer, write a function to determine if it is a power of two.
解题思路:
判断方法主要依据2的N次幂的特点:仅有首位为1,其余各位都为0.
代码如下:
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回调
- XStream 用法详解 java 类与 XML 互换
- java多文件上传plupload控件实现多图片上传(一)
- java加密解密和编码转换
- SpringMVC multipart文件上传
- Struts2开发入门
- Java for Web学习笔记(十二):JSP(2)Tag:directive,declaration,scriptlet,expression
- Java 数据类型总结
- Eclipse常用小知识汇总
- java.sql.SQLException: Io exception: Connection reset
- Java中对List集合排序的两种方法
- java中使用XStream实现将对象转为xml格式字符串或者将xml格式的字符串转换为对象
- eclipse配置springloaded实现热部署
- java虚拟机简介
- Java20: 俄罗斯方块
- Spring事务管理高级应用难点剖析之多线程的困惑
- jenkins的配置(安全 maven git jdk)
- java中的 FileWriter类 和 FileReader类的一些基本用法包括对于异常的处理
- 25个Java机器学习工具&库
- 配置ant编译时的jdk版本