【LeetCode】Power of Two
问题描写叙述
Given an integer, write a function to determine if it is a power of two.
意:推断一个数是否是2的n次幂
算法思想
假设一个数小于或等于0。一定不是2的幂次数
假设一个大于0且数是2的n次幂,则其的二进制形式有且仅有一个1,反之成立。
算法实现
public class Solution {
public boolean isPowerOfTwo(int n) {
if(n<=0)
return false;
int i = 0;
int countBit = 0;
while(i < 32){
if((n&(1<<i))!=0)
countBit++;
i++;
}
if(countBit != 1)
return false;
return true;
}
}
算法时间
T(n)=O(1)
演示结果
public static void main(String [] args){
int n = 4;
Solution s = new Solution();
System.out.println(s.isPowerOfTwo(n));
}
true
【LeetCode】Power of Two的更多相关文章
- 【LeetCode】数学(共106题)
[2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...
- 【LeetCode】位运算 bit manipulation(共32题)
[78]Subsets 给了一个 distinct 的数组,返回它所有的子集. Example: Input: nums = [,,] Output: [ [], [], [], [,,], [,], ...
- 【LeetCode】90. Subsets II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
随机推荐
- Android(Xamarin)之旅(四)
这么晚了,可能也因为一点事情,就说打开博客园看看,结果 http://www.cnblogs.com/mindwind/p/5125248.html 这篇文章瞬间吸引了我.暗暗的回想一下,十年之后,我 ...
- VB.NET上传附件代码
'附件添加 按钮 点击事件 吴翰哲 2013年7月23日 16:53:19 Protected Sub BtnAddFile_Click(ByVal sender As Object, ByVal e ...
- Repeater 使用方法
ItemTemplate: 包含要逐一呈现给数据源中的每个数据项的 HTML 元素和控件 AlternatingItemTemplate: 包含要逐一呈现给数据源中的其他每个数据项的 HTML 元素和 ...
- [数据结构] N皇后问题
代码: #include <iostream> #include <string.h> #include <algorithm> using namespace s ...
- canvas放射性渐变填充
今天在学习canvas时,遇到canvas的fillstyle有一个createRadialGradient()方法,创建放射性渐变. 上代码: <!DOCTYPE html> <h ...
- IIS Connection Timeout vs httpRuntime executionTimeout
IIS Connection Timeout specifies how long, in seconds, should the code wait before timing out from t ...
- 基于OWIN WebAPI 使用OAuth授权服务【客户端模式(Client Credentials Grant)】
适应范围 采用Client Credentials方式,即应用公钥.密钥方式获取Access Token,适用于任何类型应用,但通过它所获取的Access Token只能用于访问与用户无关的Open ...
- 用word-break: break-all解决不正确换行问题
这个在新闻频道存在已久的问题,今天终于把它给解决了! 问题是这样的,当一段文字中有比较长的链接地址,会造成链接地址之前的文字不能正确换行,效果如下: 对应的html代码如下: <p> 如图 ...
- Kali Linux Web 渗透测试视频教程— 第十六课-拒绝服务攻击
Kali Linux Web 渗透测试视频教程— 第十六课-拒绝服务攻击 文/玄魂 目录 Kali Linux Web 渗透测试视频教程— 第十六课-拒绝服务攻击................... ...
- JSP的那些事儿(2)---- DWR2.0 的配置和使用
JSP的那些事儿(2)----DWR2.0 的配置和使用 分类: Web开发 JAVA 2009-04-23 15:43 999人阅读 评论(0) 收藏 举报 jspdwrjavascriptserv ...