Python [Leetcode 342]Power of Four
题目描述:
Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
Example:
Given num = 16, return true. Given num = 5, return false.
解题思路:
位操作。
首先判断是不是只有一位数字为1,其余为0
然后判断为1的位置是不是奇数位
代码如下:
class Solution(object):
def isPowerOfFour(self, num):
"""
:type num: int
:rtype: bool
"""
if num & (num - 1) != 0:
return False
if num & 0x55555555 == 0:
return False return True
Python [Leetcode 342]Power of Four的更多相关文章
- [LeetCode] 342. Power of Four 4的次方数
Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Giv ...
- 【leetcode❤python】342. Power of Four
#-*- coding: UTF-8 -*- class Solution(object): def isPowerOfFour(self, num): ""& ...
- LeetCode 342. Power of Four (4的次方)
Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Giv ...
- LeetCode 342. Power of Four
Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Giv ...
- Leetcode 342 Power of Four 数论
题意:判断一个数是不是4的幂数,和Power of two类似. 先判断num是否大于0,再判断num是否能开根号,最后判断num开根号后的数是否是2^15的约数. 提示:4的幂数开根号就是2的幂数. ...
- [LeetCode] 342. Power of Four(位操作)
传送门 Description Given an integer (signed 32 bits), write a function to check whether it is a power o ...
- [LeetCode] 231 Power of Two && 326 Power of Three && 342 Power of Four
这三道题目都是一个意思,就是判断一个数是否为2/3/4的幂,这几道题里面有通用的方法,也有各自的方法,我会分别讨论讨论. 原题地址:231 Power of Two:https://leetcode. ...
- [LeetCode] 231. Power of Two 2的次方数
Given an integer, write a function to determine if it is a power of two. Example 1: Input: 1 Output: ...
- 231. Power of Two 342. Power of Four -- 判断是否为2、4的整数次幂
231. Power of Two Given an integer, write a function to determine if it is a power of two. class Sol ...
随机推荐
- HDU 2529 Shot (物理数学题)
题目 解题过程: //物理数学题 #include<stdio.h> #include<string.h> #include<algorithm> using na ...
- IOS笔记 #pragma mark的用法和作用(方便查找和导航代码)
简单的来说就是为了方便查找和导航代码用的. 下面举例如何快速的定位到我已经标识过的代码. #pragma mark 播放节拍器 - (void) Run:(NSNumber *)tick{ ...
- ExtJs之Ext.util.ClickRepeater
<!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...
- Linux多线程之互斥
题目 共要卖票20张,由命令行输入窗口数,由线程模拟窗口.每卖掉一张票,屏幕显示由几号窗口所卖,一并显示剩余票数 思路 由于票数 ticket_cnt 是全局变量,因此每当一个线程将其减一(卖出一张票 ...
- 【hdu3579-Hello Kiki】拓展欧几里得-同余方程组
http://acm.hdu.edu.cn/showproblem.php?pid=3579 题解:同余方程组的裸题.注意输出是最小的正整数,不包括0. #include<cstdio> ...
- android-non-ui-ui-thread-communications-part-5-5
This is the last post in my series regarding Android thread communications. Parts 1 through 4 are l ...
- hdu 1536 S-Nim
题意:首先输入K 表示一个集合的大小 之后输入集合 表示对于这对石子只能去除这个集合中的元素的 个数 之后输入一个m表示接下来对于这个集合要进行m次询问 之后m行 每行输入一个n 表示有 n个堆 ...
- jsp中如何用jstl实现if(){}else if(){}else{}
<c:choose> <c:when test="${条件}"> 情况1........... </c:when> <c:when tes ...
- 转:Move all SQL Server system databases at one time
Problem One task that you may need to do as a DBA is to move the system databases from one location ...
- Data Flow ->> Term Extraction
中文意思是关键词抽取,用于计算在文本中哪些词汇或者词组出现的频率最高.其实算法有两张:1)Frequency 2)TFIDF TFIDF的全称是Term Frequency and Inverse D ...