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. 题意:判断一个数是否是 ... 
随机推荐
- 【Python】 属性的 get 与 set 方法
			在C#里面,属性的get 与 set 非常简单方便. public class bird { public int age { get;set; } public bool isadult { get ... 
- 网络知识学习2---(IP地址、子网掩码)(学习还不深入,待完善)
			紧接着:网络知识学习1 1.IP地址 IP包头的结构如图 A.B.C网络类别的IP地址范围(图表) A.B.C不同的分配网络数和主机的方式(A是前8个IP地址代表网络,后24个代表主机:B是16 ... 
- PLSQL数据库操作(excel)
			一.plsql数据库操作: 删除数据前备份一张表: create table plat_counter_def_bf as select * from plat_monitor_counter_def ... 
- new 等于 malloc加构造函数
			1.new 是c++中的操作符,malloc是c 中的一个函数 2.new 不止是分配内存,而且会调用类的构造函数,同理delete会调用类的析构函数,而malloc则只分配内存,不会进行初始化类成员 ... 
- 【CentOS】磁盘管理与vim编译器
			一.查看硬盘或目录容量 1.df [-hmkiT] -h 查看系统磁盘使用情况 -m 使用MBytes显示结果 -k 使用KBytes显示结果 -i 查看inode -T 查看Type 2 ... 
- 疑问:line-height对非文字行内块的影响
			line-height:对子元素是非文字的行内块,表现出来的不是垂直居中.目前还不知道具体细节. 可以看出来两个东西不在一行.老师的解释是line-height对非文字元素解释不一样,但是我没懂细节. ... 
- 用 TWebBrowser 查找网页上的按钮,编辑框,
			Form1.wb1.Navigate(Aurl); <table> <tr> <td style="text-align:right;">< ... 
- LightOJ1126 Building Twin Towers(DP)
			题目 Source http://www.lightoj.com/volume_showproblem.php?problem=1126 Description Professor Sofdor Al ... 
- solr连接数据库配置
			一般要搜索的信息都是被存储在数据库里面的,但是我们不能直接搜数据库,所以只有借助Solr将要搜索的信息在搜索服务器上进行索引,然后在客户端供客户使用. 一.链接数据库 1. SQL配置 拿SQL Se ... 
- bzoj 3791: 作业
			Description 众所周知,白神是具有神奇的能力的. 比如说,他对数学作业说一声“数”,数学作业就会出于畏惧而自己完成:对语文作业说一声“语”,语文作业就会出于畏惧而自己完成. 今天,语文老师和 ... 
