Given an integer, write a function to determine if it is a power of two.
Input: 1 Output: true Explanation: 20 = 1
Input: 16 Output: true Explanation: 24 = 16
Input: 218 Output: false
impl Solution {
pub fn is_power_of_two(n: i32) -> bool {
if n <= 0 {
false
} else {
n & (n - 1) == 0
}
}
}
impl Solution {
pub fn is_power_of_two(n: i32) -> bool {
if n <= 0 {
false
} else if n == 1 {
true
} else {
n % 2 == 0 && Self::is_power_of_two(n / 2)
}
}
}