examination questions

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

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) { int t;
int b; if (n == || n == ){
return true;
}
if (n <= ){
return false;
} while (true){
t = n / ;
b = n % ;
if (b == ){
if (t == ){
return true;
}
else{
n = t;
}
}
else{
return false;
}
}
}
};

O(1) Check Power of 2 - LintCode的更多相关文章

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

    142. O(1) Check Power of 2[easy] Using O(1) time to check whether an integer n is a power of 2. Have ...

  2. Lintcode: O(1) Check Power of 2

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

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

  4. LintCode刷题笔记-- O(1) Check Power of 2

    标签: 位运算 题目: Using O(1) time to check whether an integer n is a power of 2. 解题思路: 这道题是利用位运算判断一个数是不是2 ...

  5. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  6. Power of Two & Power of Three & Power of Four

     Check Power of 2 Using O(1) time to check whether an integer n is a power of 2. Example For n=4, re ...

  7. lintcode:1-10题

    难度系数排序,容易题1-10题: Cosine Similarity new  Fizz Buzz  O(1)检测2的幂次  x的平方根  不同的路径  不同的路径 II  两个字符串是变位词  两个 ...

  8. LintCode刷题笔记-- Count1 binary

    标签: 位运算 描述: Count how many 1 in binary representation of a 32-bit integer. 解题思路: 统计一个int型的数的二进制表现形式中 ...

  9. DELL_LCD错误提示代码

    代码 文本 原因E1000 Failsafe voltage error. Contact support.(故障保护电压错误.请联络支持人员.) 查看系统事件记录以了解严重故障事件.E1114 Am ...

随机推荐

  1. Android课程---Android设置透明效果的三种方法(转)

    1.使用Android系统自带的透明效果资源 <Button  android:background="@android:color/transparent"/>   ...

  2. IOS第11天(3:UIPickerView省市联动)

    ********* #import "ViewController.h" #import "Province.h" @interface ViewControl ...

  3. Python CRC16校验算法

    def crc16(x, invert): a = 0xFFFF b = 0xA001 for byte in x: a ^= ord(byte) for i in range(8): last = ...

  4. HDU 1828 Picture(线段树扫描线求周长)

    Picture Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  5. Codevs 1021 玛丽卡

    Codevs 1021 玛丽卡 题目地址:http://codevs.cn/problem/1021/ 题目描述 Description 麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他 ...

  6. Linux系统下Redis安装(一)

    最近项目要使用Redis,特将这段时间将Redis的学习经验与大家分享,算是对这段时间学习成果的总结和技术提炼,不足之处还望大家批评指正. 项目背景: 有些很少改动的数据和经常使用的数据(例如系统中下 ...

  7. Selenium2学习-038-firefox、webdriver版本不对称问题解决:org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055

    今天有个朋友在群里问,为何脚本运行不通过,其脚本操作步骤简单描述如下: 1.启动火狐浏览器 2.打开百度 3.查询框输入关键字 4.点击按钮[百度一下] 脚本挺简单的,其给出的应用报错信息如下所示: ...

  8. django book querysets

    from __future__ import unicode_literals from django.db import models from django.contrib.auth.models ...

  9. python 安装easy_install和pip

    做个记录 easy_install和pip都是用来下载安装Python一个公共资源库PyPI的相关资源包的 首先安装easy_install 下载地址:https://pypi.python.org/ ...

  10. Flume -- 开源分布式日志收集系统

    Flume是Cloudera提供的一个高可用的.高可靠的开源分布式海量日志收集系统,日志数据可以经过Flume流向需要存储终端目的地.这里的日志是一个统称,泛指文件.操作记录等许多数据. 一.Flum ...