---
# System prepended metadata

title: \#342 Power of Four
tags: [leetcode, leetcode-easy]

---

# \#342 Power of Four
## *檢查n是否為4的次方數(n=4^x)*
## Log
 - build 20211030 by syhuang

## 二進位檢查法
 - leetcode submit runtime 74%, memory 26%
```javascript=
var isPowerOfFour = function(n) {
    return /^1(00)*$/.test(n.toString(2));
};
```
## 初見
 - edge case n=0 return false
 - 可被4整除->檢查是否為2的次方數
 - leetcode submit runtime 94%, memory 26%
```javascript=
var isPowerOfFour = function(n) {
    if(n==0) return false;
    if(n==4 || n==1) return true;
    else if(n%4==0){
        let t = n;
        while(t%4 == 0) t/=4;
        return (t==1)?true:false;
    }
    else return false;
};
```
## 備註
## 參考
###### tags: `leetcode`, `leetcode-easy`