diff options
Diffstat (limited to 'other/power_of_four.rs')
-rw-r--r-- | other/power_of_four.rs | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/other/power_of_four.rs b/other/power_of_four.rs new file mode 100644 index 0000000..f20335f --- /dev/null +++ b/other/power_of_four.rs @@ -0,0 +1,24 @@ +//INCOMPLETE +struct Solution; + +impl Solution { + pub fn is_power_of_four(num: i32) -> bool { + if num < 0 { + return false; + } + let (ans, pos, ones) = (0, 1, 0); + while num { + if num & 1 { + ans += 1; + ones = pos; + } + num >>= 1; + pos += 1; + } + ans == 1 && ones & 1 + } +} + +fn main() { + print!("{}", Solution::is_power_of_four(16)); +} |