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.
Follow up: Could you solve it without loops/recursion?
解法1,经典的数学解法:
class Solution(object):def isPowerOfFour(self, num):""":type num: int:rtype: bool"""if num <= 0: return Falsen = int(round(math.log(num, 4)))return 4**n == num
解法2,迭代:
class Solution(object):def isPowerOfFour(self, num):""":type num: int:rtype: bool"""if num <= 0: return Falsewhile num >= 4:if num % 4 != 0:return Falsenum = num / 4 return num == 1
解法3,最牛叉,
class Solution(object):def isPowerOfFour(self, num):""":type num: int:rtype: bool"""return num > 0 and (num & (num - 1)) == 0 and (num - 1) % 3 == 0
因为,4^n - 1 = C(n,1)*3 + C(n,2)*3^2 + C(n,3)*3^3 +.........+ C(n,n)*3^n
i.e (4^n - 1) = 3 * [ C(n,1) + C(n,2)*3 + C(n,3)*3^2 +.........+ C(n,n)*3^(n-1) ]
This implies that (4^n - 1) is multiple of 3.
类似解法:
return n & (n-1) == 0 and n & 0xAAAAAAAA == 0
或者是:
class Solution(object):def isPowerOfFour(self, n):""":type num: int:rtype: bool""" #1, 100, 10000, 1000000, 100000000, ....#1, 100 | 10000 | 1000000 | 100000000, ... = 0101 0101 0101 0101 0101 0101 0101 0101return n > 0 and n & (n-1) == 0 and (n & 0x55555555 != 0)
因为n & n-1 == 0 就可以确定只有1个1, so 只要保证1的位置在1,3,5,7,。。。。这些位置上就行。
转载于:https://www.cnblogs.com/bonelee/p/9206525.html
leetcode 342. Power of Four相关推荐
- [leetcode] 342. Power of Four
题目 Given an integer (signed 32 bits), write a function to check whether it is a power of 4.Example: ...
- [LeetCode] 342. Power of Four(位操作)
传送门 Description Given an integer (signed 32 bits), write a function to check whether it is a power o ...
- [LeetCode][JavaScript]Power of Three
Power of Three Given an integer, write a function to determine if it is a power of three. Follow up: ...
- LeetCode:326. Power of Three
2019独角兽企业重金招聘Python工程师标准>>> Given an integer, write a function to determine if it is a powe ...
- leetcode 231. Power of Two
Given an integer, write a function to determine if it is a power of two. class Solution(object):def ...
- LeetCode - 231. Power of Two
Given an integer, write a function to determine if it is a power of two. Example 1: Input: 16 Output ...
- LeetCode之Power of Two
1.题目 Given an integer, write a function to determine if it is a power of two. Credits: Special thank ...
- LeetCode 342. 4的幂(位运算)
文章目录 1. 题目 2. 解题 2.1 通用解法 2.2 找规律 1. 题目 给定一个整数 (32 位有符号整数),请编写一个函数来判断它是否是 4 的幂次方. 示例 1: 输入: 16 输出: t ...
- LeetCode 326. Power of Three
题目: Given an integer, write a function to determine if it is a power of three. Follow up: Could you ...
最新文章
- Docker 17.12.0 发布
- Redis 4.x/5.x未授权访问漏洞
- 盘点大厂的那些开源项目 - 华为
- C#程序集Assembly学习随笔(增补版,附图)_AX
- 使用java IO来读写文件
- Python CGI编程
- 14 英寸与 16 英寸 MacBook Pro 应该购买哪一款,M1 Pro 还是 M1 Max Mac?
- 潘多拉固件设置ipv6_k2p下潘多拉/openwrt配置ipv6地址
- 火力全开,同时分解(切脸)多个视频
- 常用Gis通用符号库大全
- 最小二乘法曲线拟合程序matlab,最小二乘法曲线拟合_原理及matlab实现.doc
- stc12c5a60s ds1302时钟
- JavaScript 排他思想
- cla作用matlab,共轭亚油酸(CLA)为什么能减脂?90%的健身者都不懂!
- Kotlin Native - 原生平台 Hollo World!
- 回归分析过程实例(练习)
- 【图像处理】基于MATLAB FCM(模糊聚类)的侧扫声呐图像分割
- java毕业生设计高校教学资源系统计算机源码+系统+mysql+调试部署+lw
- 忘记mysql密码后如何修改密码(2022最新版详细教程保姆级)
- 关闭windows安全中心报警
热门文章
- Appium入门实例(Java)
- thttpd + Cgicc
- linux命令行之find详解
- 用python查询数据库_用python 做数据库查询
- 语言 读ini文件_让C语言的调试更加高大上
- java qq音乐接口 api,QQ音乐解析API接口更新:支持HQ,ape,flac无损音质,缓存功能
- dat关闭某进程_超详细解析!工程师必会的Linux进程间通信方式和原理
- incon函数图像c语言,[转载]c语言经典题目
- php程序监听node.js程序和go程序
- 微信小程序之ajax服务器交互及页面渲染