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 ...
随机推荐
- Python实例---抽屉后台框架分析
1.1. 抽屉框架分析 --登陆注册分析 1.2. 前台获取form表单补充知识: <!DOCTYPE html> <html lang="en"> < ...
- August 18th 2017 Week 33rd Friday
If you shed tears when you miss the sun, you also miss the stars. 如果你因为错过太阳而哭泣,你也将会错过繁星. If you have ...
- redis下的持久化保存
rdb.h rdb.c ---> 完成数据保存到临时文件,再利用rename保存到指定文件的过程: 如果需要写一个数据持久化保存的功能时,可以参考这部分代码: //rdb API int ...
- C++课堂作业(2)
github的链接: https://github.com/deepYY/object-oriented/tree/master/PAT.1025 题目 给定一个常数K以及一个单链表L,请编写程序将L ...
- 008单例、继承、final
内容:单例,类继承,final #################################################################################### ...
- js图形库
SVG.js viz.js graphviz的js实现版 raphael d3 (http://d3js.org/) JavaScript InfoVis Toolkit Flotr2 and Env ...
- java简单的工厂模式
定义:专门定义一个类来创建其他类的实例,被创建的实例通常都具有共同的父类和接口.意图:提供一个类由它负责根据一定的条件创建某一及具体类的实例 //简单工厂,存在不符合开闭原则的地方,可以在参考抽象工厂 ...
- JavaScript浏览器对象模型(BOM)之history对象
history 对象是 window 对象的属性,它保存着用户上网的记录,从窗口被打开的那一刻算起. 一.history对象的属性 可以通过判断 history.length,得到是否有历史记录和记录 ...
- SoapUI这么好,舍得不用吗?
之前尝试去学习哈SoapUI, 安装都报错,直接拖黑不用,对java开发的产品本身不感冒 后来工作上,和老外沟通,发现他们不会用xmlspy,只会SoapUI,心里都想,不学习看来不方便,然后都安装了 ...
- Java并发案例03---生产者消费者问题02
生产者消费者第二种情形 package com.maple.msb.one; public class ProducerConsumer { public static void main(Strin ...