Given an integer, write a function to determine if it is a power of two.

看一个数是不是2的幂,代码如下:

 class Solution {
public:
bool isPowerOfTwo(int n) {
if(n<=) return false;
bool isPower = false;
for(int i = ; i < n; i<<=){
if(!isPower && (i&n))
isPower = true;
else if(isPower && (i&n)){
return false;
}
}
return true;
}
};

LeetCode OJ:Power of Two(2的幂)的更多相关文章

  1. LeetCode OJ:Pow(x, n) (幂运算)

    Implement pow(x, n). 幂运算,简单的方法snag然很好实现,直接循环相乘就可以了,但是这里应该不是那种那么简单,我的做法使用到了一点递归: class Solution { pub ...

  2. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  3. leetcode 326. Power of Three(不用循环或递归)

    leetcode 326. Power of Three(不用循环或递归) Given an integer, write a function to determine if it is a pow ...

  4. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  5. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  6. LeetCode OJ学习

    一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...

  7. LeetCode OJ 297. Serialize and Deserialize Binary Tree

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  8. 备份LeetCode OJ自己编写的代码

    常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...

  9. LeetCode OJ 之 Maximal Square (最大的正方形)

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

  10. LeetCode OJ:Integer to Roman(转换整数到罗马字符)

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

随机推荐

  1. configparser模块来生成和修改配置文件

    1. 安装configparser模块 pip3 install configparser ##python2.7模块名为ConfigParser 2. 创建配置文件 import configpar ...

  2. 图片上传ie与chrome总结

    if(brow.msie)  // ie浏览器执行此代码 { // data 可以是字符串   eval("data = " + data); } else  //  其他浏览器执 ...

  3. 在Pycharm中配置Github

    Pycharm是当前进行python开发,尤其是Django开发最好的IDE.GitHub是程序员的圣地,几乎人人都在用. 本文假设你对pycharm和github都有一定的了解,并且希望在pycha ...

  4. Linux学习笔记之Linux启动引导过程

    早期时,启动一台计算机意味着要给计算机喂一条包含引导程序的纸带,或者手工使用前端面板地址/数据/控制开关来加载引导程序.尽管目前的计算机已经装备了很多工具来简化引导过程,但是这一切并没有对整个过程进行 ...

  5. SeekArc

    https://github.com/neild001/SeekArc https://github.com/imflyn/SeekArc

  6. CentOS系统下yum命令的详细使用方法

    yum是什么yum = Yellow dog Updater, Modified 主要功能是更方便的添加/删除/更新RPM包. 它能自动解决包的倚赖性问题. 它能便于管理大量系统的更新问题 yum特点 ...

  7. Cisco 交换Trunk配置

    交换Trunk配置 #进入直连端口下 config)#int f0/1 #配置trunk封装协议 config)#switchport trunk encapsulation dot1q #配置为tr ...

  8. 一篇关于cfDNA的综述

    文章题目:A Field Guide for Cancer Diagnostics using cell-free DNA: from Principles to Practice and Clini ...

  9. 原生的 promise 的局限性

    本文来自:https://ekyu.moe/article/limits-of-native-promise-and-async-await/ 众所周知,Nodejs 已原生支持 Promise 和 ...

  10. webservice的cxf和spring整合发布

    1.新建一个web项目 2.导入cxf相应的jar包,并部署到项目中 3.服务接口 package com.xiaostudy; /** * @desc 服务器接口 * @author xiaostu ...