O(1) Check Power of 2 - LintCode
examination questions
Using O(1) time to check whether an integer n is a power of 2.
For n=4, return true;
For n=5, return false;
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的更多相关文章
- 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 ...
- 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, ...
- 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 ...
- LintCode刷题笔记-- O(1) Check Power of 2
标签: 位运算 题目: Using O(1) time to check whether an integer n is a power of 2. 解题思路: 这道题是利用位运算判断一个数是不是2 ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- 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 ...
- lintcode:1-10题
难度系数排序,容易题1-10题: Cosine Similarity new Fizz Buzz O(1)检测2的幂次 x的平方根 不同的路径 不同的路径 II 两个字符串是变位词 两个 ...
- LintCode刷题笔记-- Count1 binary
标签: 位运算 描述: Count how many 1 in binary representation of a 32-bit integer. 解题思路: 统计一个int型的数的二进制表现形式中 ...
- DELL_LCD错误提示代码
代码 文本 原因E1000 Failsafe voltage error. Contact support.(故障保护电压错误.请联络支持人员.) 查看系统事件记录以了解严重故障事件.E1114 Am ...
随机推荐
- BizTalk动手实验(一)安装BizTalk Server 2010开发环境
1 课程简介 通过本课程了解BizTalk 2010的软依赖及基本的安装配置步骤,BizTalk相应的解决方案及高可用性方案可在课程的基础进行深入学习. 2 准备工作 硬件环境:CPU >2.0 ...
- Unity学习疑问记录之layer问题
在Sprite Render中有个Sorting Layer,这里可以建层,而Inspector窗口中也有个layer,也可以新建层,这2者有什么不一样呢? layer主要通过光线投射来选择性地忽略碰 ...
- ceph命令
chen@admin-node:~$ ceph --help General usage: ============== usage: ceph [-h] [-c CEPHCONF] [-i INPU ...
- shutter截图工具
安装: 1.打开ubuntu software center,搜索shutter,安装. 使用:
- 浏览器中Javascript的加载和执行
在刚学习Javascript时曾对该问题在小组内做个一次StudyReport,发现其中的基础还是值得分析的. 从标题分析,可以加个Javascript的加载和执行分为两个阶段:加载.执行.而加载即浏 ...
- 【No.5 Ionic】修改 应用名,icon,启动界面
修改 应用名 直接 修改 config.xml中的name 修改icon 和 启动界面 在resources目录有个 icon.png 和 splash.png 文件,直接把文件覆盖执行重新生成命令 ...
- vim - Putting the current file on the Windows clipboard
http://vim.wikia.com/wiki/VimTip432 command! Copyfile let @*=substitute(expand("%:p"), '/' ...
- 将编码从GB2312转成UTF-8的方法汇总(从前台、程序、数据库)
这篇文章主要介绍了将编码从GB2312转成UTF-8的方法汇总(从前台.程序.数据库),需要的朋友可以参考下 一个网站如果需要国际化,就需要将编码从GB2312转成UTF-8,其中有很多的问题需要注意 ...
- iOS逆传值的三种方式
1.代理 2.block 2.通知中心
- Leetcode: Number of Boomerangs
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...