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 ...
随机推荐
- 只 一行显示可左右滚动的文本(UITextField中文限制)
// // ViewController.m // 一行显示可滚动的文本 // // Created by apple on 15-5-8. // Copyright (c) 2015年 apple. ...
- VS2010中出现无法嵌入互操作类型
针对word或excel操作时,出现VS2010中,无法嵌入互操作类型“……”,请改用适用的接口的解决方法 问了度娘,解决方法如出一辙:选中项目中引入的dll,鼠标右键,选择属性,把“嵌入互操作类型” ...
- Windows SEH学习 x86
windows 提供的异常处理机制实际上只是一个简单的框架.我们通常所用的异常处理(比如 C++ 的 throw.try.catch)都是编译器在系统提供的异常处理机制上进行加工了的增强版本.这里先抛 ...
- MongoDB (十) MongoDB Limit/限制记录
Limit() 方法 要限制 MongoDB 中的记录,需要使用 limit() 方法. limit() 方法接受一个数字型的参数,这是要显示的文档数. 语法: limit() 方法的基本语法如下 & ...
- MongoDB (七) MongoDB 数据类型
MongoDB支持许多数据类型的列表下面给出: String : 这是最常用的数据类型来存储数据.在MongoDB中的字符串必须是有效的UTF-8. Integer : 这种类型是用来存储一个数值.整 ...
- Project Euler 109 :Darts 飞镖
Darts In the game of darts a player throws three darts at a target board which is split into twenty ...
- Intellij编译时报“java: System Java Compiler was not found in classpath” 解决办法
Intellij编译时报“java: System Java Compiler was not found in classpath” 解决方法: Project Settings > Comp ...
- iOS & Mac 调试命令(VMMap&Top)
vmmap & top命令解析内存 Is there any way to query the address space of a process on Mac? On Windows, w ...
- SQL语句 递归
--正向递归查询(根据ID查到自己和自己以下的所有数据) connect by prior a.id = a. parentid --反向递归查询(根据叶子ID查出自己和自己以上的根数据) ...
- SSIS ->> Control Flow And Data Flow
In the Control Flow, the task is the smallest unit of work, and a task requires completion (success, ...