142. O(1) Check Power of 2【easy】

Using O(1) time to check whether an integer n is a power of 2.

Have you met this question in a real interview?

Yes
Example

For n=4, return true;

For n=5, return false;

Challenge

O(1) time

解法一:

 class Solution {
public:
/*
* @param n: An integer
* @return: True or false
*/
bool checkPowerOf2(int n) {
if (n > && (n & n - ) == ) {
return true;
} return false;
}
};

142. O(1) Check Power of 2【easy】的更多相关文章

  1. 142. O(1) Check Power of 2【LintCode by java】

    Description Using O(1) time to check whether an integer n is a power of 2. Example For n=4, return t ...

  2. 142. Linked List Cycle II【easy】

    142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...

  3. 160. Intersection of Two Linked Lists【easy】

    160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...

  4. 234. Palindrome Linked List【easy】

    234. Palindrome Linked List[easy] Given a singly linked list, determine if it is a palindrome. Follo ...

  5. 27. Remove Element【easy】

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

  6. 459. Repeated Substring Pattern【easy】

    459. Repeated Substring Pattern[easy] Given a non-empty string check if it can be constructed by tak ...

  7. 170. Two Sum III - Data structure design【easy】

    170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...

  8. 206. Reverse Linked List【easy】

    206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...

  9. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

随机推荐

  1. luogu P2024 食物链

    题目描述 动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形.A 吃 B,B 吃 C,C 吃 A. 现有 N 个动物,以 1 - N 编号.每个动物都是 A,B,C 中的一种,但是我 ...

  2. [BZOJ 2115] Xor

    Link:https://www.lydsy.com/JudgeOnline/problem.php?id=2115 Algorithm: 此题一看到是求异或和最大问题的,立即想到使用线性基解题 最终 ...

  3. Spoj Query on a tree III

    题目描述 给出N个点的一棵树(N-1条边),节点有白有黑,初始全为白 有两种操作: 0 i : 改变某点的颜色(原来是黑的变白,原来是白的变黑) 1 v : 询问1到v的路径上的第一个黑点,若无,输出 ...

  4. 【动态规划】bzoj1649 [Usaco2006 Dec]Cow Roller Coaster

    很像背包. 这种在一个数轴上进行操作的题常常需要对区间排序. f[i][j]表示距离到i时,花费为j时的权值之和. f[x[i]+l[i]][j+c[i]]=max{f[x[i]][j]+w[i]}( ...

  5. angualrjs2教程

    1.一本本开源的Angular2书籍:https://zhangchen915.gitbooks.io/angular2-training/ 2.好的博客教程,讲的通俗易懂:http://codin. ...

  6. 【Git】GitHub for Windows使用(1)

    目录 1.下载GitHub for windows 客户端 2.注册GitHub 3.启动windows端客户端,登录帐号 4.了解Git客户端,从而了解Git的大体功能 5.创建一个新的资源库  6 ...

  7. 【JAVA】StringTokenizer 迭代方式对字符串进行分割

    StringTokenizer是一个用来分隔String的应用类,相当于VB的split函数.1.构造函数public StringTokenizer(String str)public String ...

  8. infer 编译代码审查命令记录

    infer -- xcodebuild -target <target name> -configuration <build configuration> -sdk ipho ...

  9. 香蕉派 Banana pi BPI-M1+ 双核开源单板计算机. 板载WIFI

     Banana PI BPI-M1+是一款高性能双核开源硬件单板计算机,Banana PI BPI-M1+是一款比树莓派更强悍的双核Android4.4与Linux产品. Banana PI BP ...

  10. 基于python的ardrone control源码分析与心得

    这里有一段python代码,可用于操控ardrone 2.0.实验室曾经借鉴用过,并添加了部分功能.如今复习一下,顺便理理python的相关知识点. #!/usr/bin/env python # A ...