Skip to content

Instantly share code, notes, and snippets.

@Odomontois
Created March 22, 2021 07:53
Show Gist options
  • Save Odomontois/645ae6305b107ca28c55a9394a517844 to your computer and use it in GitHub Desktop.
Save Odomontois/645ae6305b107ca28c55a9394a517844 to your computer and use it in GitHub Desktop.
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