【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-十大经典排序算法 
随机推荐
- vb.net加密解密方法
			1.vb.net加密解密方法 Private Function getLicenseDate() As String Dim b() As Byte Dim path As String = Serv ... 
- <大学祭>
			我将20岁青涩的身影留在教室前的花坛边,远赴他乡,这些年过去了,他总是对我说别忘记我的梦: 我将那个沉静娇小的眼镜女孩印象留在第一排靠窗的座位,带着淡淡离愁而去,这些年过去了,她对我说,永恒的爱是心中 ... 
- git -- 如何解决冲突
			遇到冲突,首先要编辑冲突文件,可以使用vim或者其他工具,冲突文件变现为: <<<<HEAD 到 ==== :代表本地分支的修改内容 ==== 到 >>>&g ... 
- DOS命令大全(经典收藏)
			net use \\ip\ipc$ " " /user:" " 建立IPC空链接 net use \\ip\ipc$ "密码" /user: ... 
- asp.net 禁用按钮防止重复提交
			按钮设置 1.OnClientClick属性为”this.disabled=true;“ 2.UseSubmitBehavior属性为”false“ 举例如下: <asp:Button ID=& ... 
- Windows® 10 Mobile Technical Preview升级方法
			就在今天凌晨,微软放出了Windows 10 Mobile Technical Preview的升级,喜欢吃螃蟹的人总是希望可以在第一时间尝试新的系统,我也不例外. 本次升级涵盖了从Lumia 520 ... 
- MyEclipse 10 中如何更改字体
			打开Myeclipse软件.window->preferences->General->Appearance->Colors and Fonts然后在窗口的右边会显示一些如下图 ... 
- Android--Activity
			1.Activity:它是一种包含用户界面的组件,主要用于和用户进行交互: Activity的用法与步骤: 1)新建类MyActivity.java,继承Activity类: 2)创建布局文件myac ... 
- 视图组件(View)
			1.Android应用的大部分UI组件都放在了android.widget包及其子包,android.view包及其子包中,Android应用的所有UI组件都继承了View类2.View类还有一个重要 ... 
- 十年学会编程 著者: Peter Norvig  翻译: Dai Yuwen
			为何人人都这么着急? 信步走进任何一家书店,你会看到名为<如何在7天内学会Java>的书,还有各 种各样类似的书: 在几天内或几小时内学会Visual Basic, Windows, In ... 
