Leecode刷题之旅-C语言/python-231 2的幂
/*
* @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的幂的更多相关文章
- Leecode刷题之旅-C语言/python-1.两数之和
开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...
- Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符
/* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...
- Leecode刷题之旅-C语言/python-28.实现strstr()
/* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...
- Leecode刷题之旅-C语言/python-7.整数反转
/* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...
- Leecode刷题之旅-C语言/python-434 字符串中的单词数
/* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...
- Leecode刷题之旅-C语言/python-326 3的幂
/* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...
- Leecode刷题之旅-C语言/python-263丑数
/* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...
- Leecode刷题之旅-C语言/python-383赎金信
/* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...
- Leecode刷题之旅-C语言/python-349两整数之和
/* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...
- Leecode刷题之旅-C语言/python-349两个数组的交集
/* * @lc app=leetcode.cn id=349 lang=c * * [349] 两个数组的交集 * * https://leetcode-cn.com/problems/inters ...
随机推荐
- Server runtime
spring mvc常用的注解: 个介绍. @Controller @Controller 负责注册一个bean 到spring 上下文中,bean 的ID 默认为 类名称开头字母小写,你也可以自己指 ...
- JQuery学习---JQuery基础知识
JQuery介绍: [官网]http://jquery.com [参考API]http://jquery.cuishifeng.cn/ JQuery的低版本支持IE低版本,JQuery的2版本不太支持 ...
- Web前端——jQuery----细节
jQuery终极思想:创建jQuery对象(当发现相同的选择器在你的代码里出现多次时,请用变量把它储存起来.一切面向对象) 认识jQuery 1.jQuery库可以做什么? HTML元素选取 HTML ...
- August 06th 2017 Week 32nd Sunday
No words are necessary between two loving hearts. 两颗相爱的心之间不需要言语. No, I don't think so. Words may be ...
- [T-ARA][Sugar Free]
歌词来源:http://music.163.com/#/song?id=29343991 作曲 : 新沙洞老虎/범이낭이 [作曲 : 新沙洞老虎/버미낭이] [作曲 : 新沙洞老虎/p/beo-mi- ...
- BZOJ 1013 球形空间产生器sphere 高斯消元
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1013 题目大意: 有一个球形空间产生器能够在n维空间中产生一个坚硬的球体.现在,你被困 ...
- 使用tidylib解决不规则网页问题
windows 下安装tidylib 先去https://pypi.python.org/pypi/pytidylib 下载pytidylib 跑官方提供的测试用例报错 ition 0: unexpe ...
- pwnhub_WTP攻击思路--self-xss高级利用
1.self-xss+302跳转构造csrf的利用: 1.login.php 存在跳转2.http://54.223.108.205:23333/login.php?redirecturl=//vps ...
- js中this应用
this是js的一个关键字,随着函数使用场合不同,this的值会发生变化.但是总有一个原则,那就是this指的是调用函数的那个对象. 1.纯粹函数调用. function test() { this. ...
- 电脑需要重启才能连上WLAN
我的笔记本电脑是Windows10 系统,在某次更新后发现这个问题,查资料过程中忽然断网,非要重启才能解决,非常恼人.经过一番研究,发现一个行之有效的解决方法. 1.打开设备管理器. 2.点击网络适配 ...