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. SVN :This XML file does not appear to have any style information associated with it.

    SVN :This XML file does not appear to have any style information associated with it. The document tr ...

  2. jquery无法修改网页标题,无法修改网页标题

    今天遇到一个问题,搜索时动态修改网页标题,用jquery的这段代码无效,无论FF还是IE $("title").html("new title"); 后来只好用 ...

  3. mysql中profile的使用

    1.profile在mysql5.0.37及其以上版本中可用 2.查看profile是否可用SELECT @@profiling;来查看是否已经启用profile,如果profilng值为0,可以通过 ...

  4. jQuery获取一般处理程序(ashx)的JSON数据

    昨天有在开发的软件生产线生产流程,RFID扫描IC卡的数据,当中有用到jQuery获取一般处理程序(ashx)的JSON数据.今有把它写成一个小例子,望需要的网友能参考. 在网站中,创建一个一般应用程 ...

  5. 集合中list、ArrayList、LinkedList、Vector的区别、Collection接口的共性方法以及数据结构的总结

    List (链表|线性表) 特点: 接口,可存放重复元素,元素存取是有序的,允许在指定位置插入元素,并通过索引来访问元素 1.创建一个用指定可视行数初始化的新滚动列表.默认情况下,不允许进行多项选择. ...

  6. C++ Primer Pluse_8_课后题

    #include <iostream> #include <string> #include<cstring> using namespace std; void ...

  7. 如何用ajax提交多组同样的数据(数组)到后台?

    我在AJAX中这样写 $("#subbutton").click(function(){          var machineCode_1=$("#machineCo ...

  8. PL/SQL不支持64位Oracle Client 解决办法

    解决X64操作系统PL/SQL连接报错问题 make sure you have the 32 bits oracle client installed 说明PLSQL Developer并不支持Or ...

  9. VS2010使用TTS

    最近在写纯车牌停车管理系统,需要用到语音合成功能. 目前最常用的Windows Speech SDK版本有三种:5.1.5.3和5.4.        Windows Speech SDK 5.1版本 ...

  10. 多浏览器兼容用javascript获取url参数的方法比较推荐的一种

    多浏览器兼容用javascript获取url参数的方法比较推荐的一种 <script language = javascript> function request(paras){ var ...