/*
* @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. Unity IOC/DI使用

    一.IOC介绍 IOC(Inversion of Control),中文译为控制反转,又称为“依赖注入”(DI =Dependence Injection) IOC的基本概念是:不创建对象,但是描述创 ...

  2. asp.net生成PDF文件(一)

    这个是例子是网上淘来的,哈哈,很有用的! 首先要到网上下载itextsharp.dll,然后添加引用,主程序如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 ...

  3. Java实例---简单的超市管理系统

    代码分析 Customer.java package test; public class Customer { private String name; private int customerTy ...

  4. Exchange Server 产品路线图 及 补丁下载

    Exchange Server RU listExchange Server and Update Rollup Build Numbers -TechNet Articles -United Sta ...

  5. python3 邮件,多用户,抄送

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2017/8/19 10:44 # @Author : Lys # @Site : # ...

  6. Git commit comment 汇总标准

    参考汇总互联网其它文章建议,结合PEP 257 Docstring Conventions的描述,总结的Git 注释风格,作为个人执行的标准.内容如下: 遵循标准: 1,所有注释尽量坚持使用英文,如果 ...

  7. Java中如何利用File类递归的遍历指定目录中的所有文件和文件夹

    package cuiyuee; import java.io.File; import java.util.ArrayList; import java.util.List; public clas ...

  8. chrome浏览器Network面板请求Timing分析

    Timing显示资源在整个请求生命周期过程中各部分话费的时间. Queueing 排队的时间花费.可能由于该请求被渲染引擎认为是优先级比较低的资源(图片).服务器不可用.超过浏览器并发请求的最大连接数 ...

  9. RedisClient的安装及基本使用

    管理redis的可视化客户端目前较流行的有三个:Redis Client ; Redis Desktop Manager ; Redis Studio. 这里目前给大家介绍Redis Client 的 ...

  10. FP又称为Monadic Programming

    什么是Monad? trait Monad[+T] { def flatMap[U]( f : (T) => Monad[U] ) : Monad[U] def unit(value : B)  ...