给定一个整数,写一个函数来判断它是否是2的幂。

详见:https://leetcode.com/problems/power-of-two/description/

Java实现:

class Solution {
public boolean isPowerOfTwo(int n) {
return (n>0)&&((n&(n-1))==0);
}
}

C++实现:

class Solution {
public:
bool isPowerOfTwo(int n) {
return (n>0)&&(!(n&(n-1)));
}
};

  

231 Power of Two 2的幂的更多相关文章

  1. 231. Power of Two 342. Power of Four -- 判断是否为2、4的整数次幂

    231. Power of Two Given an integer, write a function to determine if it is a power of two. class Sol ...

  2. [LeetCode] 231 Power of Two && 326 Power of Three && 342 Power of Four

    这三道题目都是一个意思,就是判断一个数是否为2/3/4的幂,这几道题里面有通用的方法,也有各自的方法,我会分别讨论讨论. 原题地址:231 Power of Two:https://leetcode. ...

  3. Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

    1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...

  4. LeetCode - 326, 342, 231 Power of Three, Four, and Two

    1. 问题 231. Power of Two: 判断一个整数是否是2的n次方,其中n是非负整数 342. Power of Four: 判断一个整数是否是4的n次方,其中n是非负整数 326. Po ...

  5. 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 ...

  6. [LeetCode] 231. Power of Two ☆(是否2 的幂)

    描述 Given an integer, write a function to determine if it is a power of two. 给定一个整数,编写一个函数来判断它是否是 2 的 ...

  7. LeetCode - 231. Power of Two - 判断一个数是否2的n次幂 - 位运算应用实例 - ( C++ )

    1.题目:原题链接 Given an integer, write a function to determine if it is a power of two. 给定一个整数,判断该整数是否是2的 ...

  8. [LeetCode]231. Power of Two判断是不是2\3\4的幂

    /* 用位操作,乘2相当于左移1位,所以2的幂只有最高位是1 所以问题就是判断你是不是只有最高位是1,怎判断呢 这些数-1后形成的数,除了最高位,后边都是1,如果n&n-1就可以判断了 如果是 ...

  9. LeetCode 231 Power of Two

    Problem: Given an integer, write a function to determine if it is a power of two. Summary: 判断一个数n是不是 ...

随机推荐

  1. Thinkphp5.0 的实践一

    Thinkphp5.0 的实践一 tp5.0默认没有__SELF__,需要定义, define('__SELF__',strip_tags($_SERVER['REQUEST_URI'])); tp5 ...

  2. UVA 1025_A Spy in the Metro

    [题意](小紫书)一个人从站台1出发,乘车要在时刻T到达站台n,为使在站台等车时间最短,她可以选择乘坐两个方向的列车,并在客车停靠站的时候换车. [分析]每次停站下车时,她都有三种选择,1.原地不动 ...

  3. Delphi:ClientDataset+TDataSetProvider的数据保存问题

    看到一篇介绍ClientDataSet和TDataSetProvider,非常精彩,特此保存. ==================================================== ...

  4. [bzoj3527][Zjoi2014]力_FFT

    力 bzoj-3527 Zjoi-2014 题目大意:给定长度为$n$的$q$序列,定义$F_i=\sum\limits_{i<j}\frac{q_iq_j}{(i-j)^2}-\sum\lim ...

  5. codevs——6220 ZHR吃好吃的

    6220 ZHR吃好吃的  时间限制: 1 s  空间限制: 1000 KB  题目等级 : 白银 Silver 题解  查看运行结果     题目描述 Description 有一个人名字叫ZHR, ...

  6. Spring集成Redis方案(spring-data-redis)(基于Jedis的单机模式)(待实践)

    说明:请注意Spring Data Redis的版本以及Spring的版本!最新版本的Spring Data Redis已经去除Jedis的依赖包,需要自行引入,这个是个坑点.并且会与一些低版本的Sp ...

  7. JSTL-函数标签库

    主页:http://www.cnblogs.com/EasonJim/p/6958992.html的分支页. 一.fn:contains() fn:contains()函数决定了一个输入字符串是否包含 ...

  8. python操作mysql方法和常见问题

    http://www.cnblogs.com/ma6174/archive/2013/02/21/2920126.html 安装mysql模块 sudo easy_install mysql-pyth ...

  9. [React] Spread Component Props in JSX with React

    You often find duplication between the name of a prop and a variable you will assign to the prop. JS ...

  10. 华为OJ2051-最小的K个数(Top K问题)

    一.题目描述 描述: 输入n个整数,输出其中最小的k个. 输入: 输入 n 和 k 输入一个整数数组 输出: 输出一个整数数组 样例输入: 5 2 1 3 5 7 2 样例输出: 1 2 二.Top ...