/*
* @lc app=leetcode.cn id=231 lang=c
*
* [231] 2的幂
*
* https://leetcode-cn.com/problems/power-of-two/description/
*
* algorithms
* Easy (44.38%)
* Total Accepted: 13.8K
* Total Submissions: 31.2K
* Testcase Example: '1'
*
* 给定一个整数,编写一个函数来判断它是否是 2 的幂次方。
*
* 示例 1:
*
* 输入: 1
* 输出: true
* 解释: 2^0 = 1
*
* 示例 2:
*
* 输入: 16
* 输出: true
* 解释: 2^4 = 16
*
* 示例 3:
*
* 输入: 218
* 输出: false
*
*/
bool isPowerOfTwo(int n) {
if(n<= || n>)//2的幂最小是1 int最大为2147483647
return false;
while(n>){
if(n%!=) return false;
n /=;
}
return true;
}

如果除2有余数的话那肯定就不是了。

还看到大佬写的一句话:

 return log(n)/log()==(int)(log(n)/log());

但是这个 到了2的三十次方 就不对了 就差这一个 不知道为什么。

#
# @lc app=leetcode.cn id=231 lang=python3
#
# [231] 2的幂
#
# https://leetcode-cn.com/problems/power-of-two/description/
#
# algorithms
# Easy (44.38%)
# Total Accepted: 13.8K
# Total Submissions: 31.2K
# Testcase Example: '1'
#
# 给定一个整数,编写一个函数来判断它是否是 2 的幂次方。
#
# 示例 1:
#
# 输入: 1
# 输出: true
# 解释: 2^0 = 1
#
# 示例 2:
#
# 输入: 16
# 输出: true
# 解释: 2^4 = 16
#
# 示例 3:
#
# 输入: 218
# 输出: false
#
#
class Solution:
def isPowerOfTwo(self, n: int) -> bool:
return (n>0) and (n & (n-1))==0

这里 &进行与 运算,如果是2的幂的话,比如4, 4是 0100 3是0011   2是 0010   与运算结果为0 才是2的幂 不为0则不是

Leecode刷题之旅-C语言/python-231 2的幂的更多相关文章

  1. Leecode刷题之旅-C语言/python-1.两数之和

    开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...

  2. Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符

    /* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...

  3. Leecode刷题之旅-C语言/python-28.实现strstr()

    /* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...

  4. Leecode刷题之旅-C语言/python-7.整数反转

    /* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...

  5. Leecode刷题之旅-C语言/python-434 字符串中的单词数

    /* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...

  6. Leecode刷题之旅-C语言/python-326 3的幂

    /* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...

  7. Leecode刷题之旅-C语言/python-263丑数

    /* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...

  8. Leecode刷题之旅-C语言/python-383赎金信

    /* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...

  9. Leecode刷题之旅-C语言/python-349两整数之和

    /* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...

  10. Leecode刷题之旅-C语言/python-349两个数组的交集

    /* * @lc app=leetcode.cn id=349 lang=c * * [349] 两个数组的交集 * * https://leetcode-cn.com/problems/inters ...

随机推荐

  1. 微信开发平台中有个接口是上传多媒体文件,我用的是java 开发的,我怎么样才能在后台实现呢?

    /**      * 文件上传到微信服务器      * @param fileType 文件类型      * @param filePath 文件路径      * @return JSONObj ...

  2. 动态展开tableView的cell[1]

    动态展开tableView的cell[1] 源码地址:https://github.com/xerxes235/HVTableView 虽然作者写的demo很好看,可是,你很难理解他是怎么玩的-_-! ...

  3. Java实例---计算器实例

    1.计算器上的键的显示名字 1.0 继承JFrame类 public class Calculate extends JFrame { } 1.1定义常量 /** 计算器上的键的显示名字 */ pub ...

  4. Exchange Server 2016 管理邮箱收发限制

    备注:本文是Exchange Server 2016管理系列的配套课件,更加详细的讲解请参考视频课程,文章结尾有视频课程主页的链接. 进行收发邮件大小的限制是很有必要的,因为邮件服务器不能当作文件服务 ...

  5. August 21st 2017 Week 34th Monday

    In fact, the happiest fairy tale is no more than the simple days we have together. 其实全世界最幸福的童话,也比不上我 ...

  6. opencv python3.6安装和测试

    安装: 命令行  pip install D:\python3.6.1\Scriptsopencv_python-3.2.0-cp36-cp36m-win_amd64.whl 测试代码: import ...

  7. 【[SDOI2014]数数】

    被慎老师教育数位\(dp\)怎么写了 看来我数位\(dp\)的写法太落后了 这道题很显然就是一个\(AC\)自动机上的数位\(dp\),按照套路 我们可以设计\(dp[i][j][0/1]\)表示匹配 ...

  8. Web项目打成war包部署到tomcat时报MySQL Access denied for user 'root'@'localhost' (using password: YES)错误解决方案

    Web项目使用使用root账号root密码进行部署,通过Eclipse加载到Tomcat服务器可以发布成功,打成war包放到tomcat的webapps目录无法发布成功,报错: jdbc.proper ...

  9. Vue动态实现评分效果

    1.图片分为三种 on:half:  off <style> .star{ font-size: 0; } .star-item{ display: inline-block; backg ...

  10. 【vue】饿了么项目-header组件开发

    1.数据传递的理解 在App.vue中用到了header组件,首先注册组件 components: { 'v-header': header } 然后才能引用 <v-header :seller ...