问题描写叙述

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的更多相关文章

  1. 【LeetCode】数学(共106题)

    [2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...

  2. 【LeetCode】位运算 bit manipulation(共32题)

    [78]Subsets 给了一个 distinct 的数组,返回它所有的子集. Example: Input: nums = [,,] Output: [ [], [], [], [,,], [,], ...

  3. 【LeetCode】90. Subsets II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...

  4. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  5. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  6. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  7. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  8. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  9. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

随机推荐

  1. 【转】HTTP状态码(HTTP Status Code)

    原文链接:http://www.chaoji.com/features/httpstatus.aspx 一些常见的状态码为: 200 - 服务器成功返回网页 404 - 请求的网页不存在 503 - ...

  2. iOS全局调用的提示 没有网络 没有更多 等。。 短时间内自动消失

    本来想用SVProgressHUD 但是由于这个需求相对要简单 所以自己写了 下面上代码 .h 文件 #import <UIKit/UIKit.h> @interface HaveNoMo ...

  3. DataTable与List互换

    public static class List2DataTable { #region "Convert Generic List to DataTable" /// <s ...

  4. python 之 theano学习:

    (1)theano主要支持符号矩阵表达式 (2)theano与numpy中都有broadcasting:numpy中是动态的,而theano需要在这之前就知道是哪维需要被广播.针对不同类型的数据给出如 ...

  5. Docker学习笔记整理

    Docker接触有一段时间了,但是对于Docker的使用可以说是一点不会.现在要在Docker上部署基于Angular开发的页面.只能一点点积累查找的资料,顺手整理一下,方便后面的回顾. 其中用到的资 ...

  6. 跟我一起学WCF(10)——WCF中事务处理

    一.引言 好久没更新,总感觉自己欠了什么一样的,所以今天迫不及待地来更新了,因为后面还有好几个系列准备些,还有很多东西需要学习总结的.今天就来介绍下WCF对事务的支持. 二.WCF事务详解 2.1 事 ...

  7. 基于阿里云容器服务用docker容器运行ASP.NET 5示例程序

    小试阿里云容器服务 之后,接下来有一个挡不住的小试冲动--用docker容器运行程序.首先想到的程序是 ASP.NET 5示例程序,于是参考msdn博客中的这篇博文 Running ASP.NET 5 ...

  8. DDD Example

    PART 1: http://www.infoq.com/presentations/model-to-work-evans PART 2: http://www.infoq.com/presenta ...

  9. C++ Primer 快速入门

    <C++ Primer 4th> 读书摘要 必须有一个命名为 main.操作系统通过 main 函数返回的值来确定程序是否成功执行完毕.返回 0 值表明程序程序成功执行完毕.任何其他非零的 ...

  10. crossplatform---electron Quick Start

    Electron enables you to create desktop applications with pure JavaScript by providing a runtime with ...