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 ...
随机推荐
- Linux下的Mysql的远程访问
mysql的服务端[192.168.25.136] 1,在远程访问之前需先配置防火墙 service iptables stop (不推荐,可配置开通3306端口) 2,授权 mysql> gr ...
- [学习笔记] CDQ分治&整体二分
突然诈尸.png 这两个东西好像都是离线骗分大法... 不过其实这两个东西并不是一样的... 虽然代码长得比较像 CDQ分治 基本思想 其实CDQ分治的基本思想挺简单的... 大概思路就是长这样的: ...
- CSS(层叠样式表)基础知识
CSS 指层叠样式表 (Cascading Style Sheets).样式定义怎样显示 HTML 元素.它通常存储在样式表中,把样式加入到 HTML 4.0 中,解决内容与表现分离的问题. 当同一 ...
- Guava包学习--Table
Table,顾名思义,就好像HTML中的Table元素一样,其实就是行+列去确定的值,更准确的比喻其实就是一个二维矩阵. 其实它就是通过行+列两个key去找到一个value,然后它又containsv ...
- UVa 1639 - Candy(数学期望 + 精度处理)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- BZOJ4653:[NOI2016]区间(线段树)
Description 在数轴上有 n个闭区间 [l1,r1],[l2,r2],...,[ln,rn].现在要从中选出 m 个区间,使得这 m个区间共同包含至少一个位置.换句话说,就是使得存在一个 x ...
- x-frame-options、iframe与iframe的一些操作
iframe的子操作父窗口,父操作子窗口: test.php: <!DOCTYPE html> <html> <head> <title>test< ...
- js中this应用
this是js的一个关键字,随着函数使用场合不同,this的值会发生变化.但是总有一个原则,那就是this指的是调用函数的那个对象. 1.纯粹函数调用. function test() { this. ...
- Ubuntu安装docker笔记
前言 根据参考文档简单记录Ubuntu系统安装docker的步骤 系统版本 panzi@ubuntu:~$ cat /etc/issue Ubuntu 16.04.5 LTS \n \l 移除旧版 ...
- [译] MVP模式的14条规则
笔者在前文<MVP和MVC>中提到了两者的区别,以及MVP日趋流行的原因:即随着各种给力UI框架的发布,View的功能越来越强,已经足以完成一些简单的不需要与后台或其他view交互的eve ...