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 ...
随机推荐
- 【iCore3 双核心板】例程十四:FATFS实验——文件操作
实验指导书及代码包下载: http://pan.baidu.com/s/1dEpechF iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- NOJ 1074 Hey Judge(DFS回溯)
Problem 1074: Hey Judge Time Limits: 1000 MS Memory Limits: 65536 KB 64-bit interger IO format: ...
- php SPL学习
数据结构 SplDoublyLinkedList - 该SplDoublyLinkedList类提供了一个双向链表的主要功能 SplStack - 该SplStack类提供了一种使用双向链表实现栈的主 ...
- Flink - metrics
Metrics是以MetricsGroup来组织的 MetricGroup MetricGroup 这就是个metric容器,里面可以放subGroup,或者各种metric 所以主要的接口就是注 ...
- kuohao
#include <stdio.h> int b[50]; int a[50]; int w[50]; int main() { freopen("in.txt",&q ...
- 001_从原理上搞定编码-- Base64编码
开发者对 Base64编码肯定很熟悉,是否对它有很清晰的认识就不一定了.实际 上Base64已经简单到不能再简单了,如果对它的理解还是模棱两可实在不应该.大概介绍一下Base64的相关内容,花几分钟时 ...
- iOS下Html页面中input获取焦点弹出键盘时挡住input解决方案—scrollIntoView()
问题描述 iOS系统下,移动web页面,inpu获取焦点弹出系统虚拟键盘时,偶尔会出现挡住input的情况,尽管概率不大,但是十分影响用户体验. 问题重现 原始页面:页面中有header.main.f ...
- 《转》Spring4 Freemarker框架搭建学习
这里原帖地址:http://www.cnblogs.com/porcoGT/p/4537064.html 完整配置springmvc4,最终视图选择的是html,非静态文件. 最近自己配置spring ...
- Did not find handler method for springMVC资源文件扫描不到---关于spring的那些坑
今天将项目的spring版本升级到4.2.5版本后,登录首页发现资源文件全部访问不到,页面彻底挂掉: 查找原因,后来又查找spring的更新文档后,才确认下来原来是mvc-dispatcher-ser ...
- App_global.asax.pdb: 另一个程序正在使用此文件,进程无法访问。
页面修改后,浏览报错,信息如下. 编译错误 说明: 在编译向该请求提供服务所需资源的过程中出现错误.请检查下列特定错误详细信息并适当地修改源代码. 编译器错误消息: CS0042: 创建调试信息文件“ ...