--- title: 'LeetCode 342. Power of Four' disqus: hackmd --- # LeetCode 342. Power of Four ## Description Given an integer n, return true if it is a power of four. Otherwise, return false. An integer n is a power of four, if there exists an integer x such that n == 4^x^. ## Example Input: n = 16 Output: true Input: n = 1 Output: true ## Constraints -2^31^ <= x <= 2^31^ - 1 ## Answer 4的次方必是2的次方,先將小於0和非2次方篩掉,4的次方數-1必整除3,故檢查餘數為0者為答案。 ```Cin= //2021_11_09 bool isPowerOfFour(int num) { if((num & (num - 1)) || num < 1){return false;} return ((num - 1) % 3) == 0; } ``` ## Link https://leetcode.com/problems/power-of-four/ ###### tags: `Leetcode`