Created
March 22, 2021 07:53
-
-
Save Odomontois/645ae6305b107ca28c55a9394a517844 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::iter::successors; | |
lazy_static! { | |
static ref POWS: Vec<u64> = (0..30).map(|x| 1 << x).map(digit_map).collect(); | |
} | |
impl Solution { | |
pub fn reordered_power_of2(n: i32) -> bool { | |
POWS.contains(&digit_map(n)) | |
} | |
} | |
fn digit_map(n: i32) -> u64 { | |
successors(Some(n), |&x| Some(x / 10)) | |
.take_while(|&x| x > 0) | |
.map(|x| x % 10) | |
.fold(0, |m, c| { | |
let mask = 0b1111 << 4 * c; | |
let count = ((m & mask) >> 4 * c) + 1; | |
m & (!0 ^ mask) | (count << 4 * c) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment