【Bit Operation-Simple】342.4 Power

[Title]
Given an integer, write a function to determine whether it is a power of 4. If yes, return true; otherwise, return false.
If the integer n is a power of 4, it must satisfy: there is an integer x such that n == 4x
[Example 1]
Input: n = 16
Output: true
[Example 2]
Input: n = 5
Output: false
[Example 3]
Input: n = 1
Output: true
[Prompt]
-231 <= n <= 231-1
[Advanced]
Can you complete this question without using loops or recursion?
[Code]
[Python]
Execution time:
44 ms, defeated 47.29% of users in all Python3 submissions
Memory consumption:
14.8 MB, defeated 32.64% of users in all Python3 submissions

class Solution:
    def isPowerOfFour(self, n: int) -> bool:
        if n<=0:
            return False
        s=bin(n)[2:]
        t=s.count('0')
        y=s.count("1")
        return n==1 or (t%2==0 and t!=0 and y==1)

[Method 2]
Execution time:
40 ms, defeated 71.07% of users in all Python3 submissions
Memory consumption:
14.7 MB, defeated 86.44% of users in all Python3 submissions

class Solution:
    def isPowerOfFour(self, n: int) -> bool:
        if n<=0:
            return False
        s=bin(n)[2:]
        dic=dict(Counter(s))
        return  dic['1']==1 and dic.setdefault('0',0)%2==0

[Method 3: Pre-calculation]
Insert picture description here

class Solution:
    def isPowerOfFour(self, n: int) -> bool:
        rs=[1,4,16,64,256,1024,4096,16384,65536,262144,1048576,4194304,16777216,67108864,268435456,1073741824]
        return n in rs

[Method 4: Mathematical Method]
Insert picture description here

class Solution:
    def isPowerOfFour(self, n: int) -> bool:
        return n>0 and log2(n)%2==0

[Method 5: Bit operation ]

  • We first check whether num is a power of 2: x> 0 and x & (x-1) == 0
  • The problem now is to distinguish even powers of 2 (when xx is a power of 4) and odd powers of 2 (when xx is not a power of 4). In binary representation, only one bit is 1 in both cases, and the rest are 0.
  • In the first case (power of 4), 1 is in the even position: 0th, 2nd, 4th, etc.; in the second case, 1 is in the odd position.
  • Therefore, the power of 4 and the number (101010…10) will get 0 in 2 directions. That is, 4 a (101010…10)2 == 0, 4 a ∧(101010…10) 2 == 0.
  • (101010…10) 2 is expressed in hexadecimal as: (aaaaaaaa)16
    Insert picture description here
    Insert picture description here
class Solution:
    def isPowerOfFour(self, n: int) -> bool:
        return n>0 and n&(n-1)==0 and n&0xaaaaaaaa==0

[Method 6: Bit Operation + Math Operation]

  • First calculate whether n is the power of x, n&(n-1)==0
  • n=2 a At this time, there are two cases, a is an even number, a is an odd number, if n is a power of 4, then a is an even number
  • When a=2x, 2 2x mod 3=4 x mod 3=((3+1) x mod 3)=1
  • When a=2x+1, 2 2x+1 mod 3=(2 * 4 x mod 3)=(2*(3+1) x mod 3)=2
  • To sum up: When n is a power of 2, and n mod 3==1, n is a power of 4.
    Insert picture description here
class Solution:
    def isPowerOfFour(self, n: int) -> bool:
        return n>0 and n&(n-1)==0 and n%3==1

Guess you like

Origin blog.csdn.net/kz_java/article/details/115154966