Skip to content

Latest commit

 

History

History
44 lines (38 loc) · 837 Bytes

File metadata and controls

44 lines (38 loc) · 837 Bytes

342. Power of Four

Given an integer (signed 32 bits), write a function to check whether it is a power of 4.

Example 1:

Input: 16
Output: true

Example 2:

Input: 5
Output: false

Follow up: Could you solve it without loops/recursion?

Solutions (Rust)

1. num div 4

impl Solution {
    pub fn is_power_of_four(num: i32) -> bool {
        let mut num = num;
        if num <= 0 {
            false
        } else {
            while num % 4 == 0 {
                num /= 4;
            }
            num == 1
        }
    }
}

2. Bitwise Operator

impl Solution {
    pub fn is_power_of_four(num: i32) -> bool {
        (num > 0) && (num & 0x55555555 == num) && (num & (num - 1) == 0)
    }
}