LeetCode Power of Four
原题链接在这里:https://leetcode.com/problems/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?
题解:
与Power of Two, Power of Three类似。
每次iteration若是不能被4整除即return false. 若可以被4整除,便除以4, 直到结果等于1.
Time Complexity: O(log(num)). Space: O(1).
AC Java:
 public class Solution {
     public boolean isPowerOfFour(int num) {
         if(num<=0){
             return false;
         }
         while(num%4 == 0){
             num /= 4;
         }
         return num==1;
     }
 }
Follow up 需要no loops/recursion.
可以bit manipulation, 首先判定num是否为2的幂数. 若是2的幂数, num二进制表达首位为1, num-1除首位均为1. num & num-1 应等于 0.
然后判定首位的1是在奇数位置上, eg. 10000 = 16. 所以 num & 0x55555555 应等于num 原值.
Time Complexity: O(1). Space: O(1).
 public class Solution {
     public boolean isPowerOfFour(int num) {
         return num>0 && (num & num-1) == 0 && (num & 0x55555555) == num;
     }
 }
LeetCode Power of Four的更多相关文章
- [LeetCode] Power of Four 判断4的次方数
		
Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Gi ...
 - [LeetCode] Power of Three 判断3的次方数
		
Given an integer, write a function to determine if it is a power of three. Follow up:Could you do it ...
 - [LeetCode] Power of Two 判断2的次方数
		
Given an integer, write a function to determine if it is a power of two. Hint: Could you solve it in ...
 - LeetCode Power of Three
		
原题链接在这里:https://leetcode.com/problems/power-of-three/ 与Power of Two类似.检查能否被3整除,然后整除,再重复检查结果. Time Co ...
 - Leetcode Power of Two
		
Given an integer, write a function to determine if it is a power of two. 题目意思: 给定一个整数,判断是否是2的幂 解题思路: ...
 - Leetcode Power of two, three, four
		
Given an integer, write a function to determine if it is a power of two. Hint: Could you solve it in ...
 - leetcode power(x,n)
		
class Solution { public: double pow(double x, int n) { double a=1; if(n==0)return 1; if(x==1)return ...
 - LeetCode——Power of Two
		
Description: Given an integer, write a function to determine if it is a power of two. public class S ...
 - [LeetCode]Power of N
		
题目:Power of Two Given an integer, write a function to determine if it is a power of two. 题意:判断一个数是否是 ...
 
随机推荐
- Redis到底该如何利用(二)?
			
上一篇文章里我简述了使用Keys作为Redis搜索的方式,确实感受到了社区的力量,写文章好处多.首先谢谢各位前辈的指导,我知道了拿Redis作为搜索是个错误的方向.本来这篇文章我觉得确实没必要发了,但 ...
 - ubuntu下配置jdk
			
1.首先下载jdk-7u51-linux-i586.tar.gz.并将它放在例如/home目录. 2.解压安装 sudo tar zxvf ./jdk-7u51-linux-i586.tar.gz ...
 - Hadoop学习笔记(1) 初识Hadoop
			
1. Hadoop提供了一个可靠的共享存储和分析系统.HDFS实现存储,而MapReduce实现分析处理,这两部分是Hadoop的核心. 2. MapReduce是一个批量查询处理器,并且它能够在合理 ...
 - js动态加载以及确定加载完成的代码
			
利用原生js动态加载js文件到页面,并在确定加载完成后调用相关function var otherJScipt = document.createElement("script") ...
 - DIV的Position属性和DIV嵌套DIV
			
1.前言 我们在利用div+css进行布局时,常常被div的位置弄的焦头烂额,很多人甚至放弃了div而直接用table.这里一如既往的推荐使用div布局,其实我们只要掌握了div的position属性 ...
 - ACM:    SCU 4440 Rectangle - 暴力
			
SCU 4440 Rectangle Time Limit:0MS Memory Limit:0KB 64bit IO Format:%lld & %llu Practic ...
 - 基于UDP协议的socket编程示例
			
客户端 import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; impo ...
 - 【BZOJ】3993: [SDOI2015]星际战争
			
题意 \(m\)个人\(n\)个物品,第\(i\)个物品生命值为\(A_i\),第\(i\)个人每秒可以减少一个物品\(B_i\)的生命值,给出一个\(m \times n\)的矩阵,如果\(i\)行 ...
 - oracle 数据库 时间差 年数、月数、天数、小时数、分钟数、秒数
			
declare l_start date := to_date('2015-04-29 01:02:03', 'yyyy-mm-dd hh24:mi:ss'); l_end date := to_da ...
 - ActiveMQ集群应用
			
ActiveMQ集群 ActiveMQ具有强大和灵活的集群功能,但在使用的过程中会发现很多的缺点,ActiveMQ的集群方式主要由两种:Master-Slave和Broker Cluster. 1.M ...