
g0301_0400.s0338_counting_bits.Solution.rs Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
// #Easy #Dynamic_Programming #Bit_Manipulation #Udemy_Bit_Manipulation
// #Big_O_Time_O(num)_Space_O(num) #2024_09_12_Time_0_ms_(100.00%)_Space_2.6_MB_(15.06%)
impl Solution {
pub fn count_bits(num: i32) -> Vec {
let mut result = vec![0; (num + 1) as usize];
let mut border_pos = 1;
let mut incr_pos = 1;
for i in 1..=num {
// When we reach a power of 2, reset border_pos and incr_pos
if incr_pos == border_pos {
result[i as usize] = 1;
incr_pos = 1;
border_pos = i;
} else {
result[i as usize] = 1 + result[incr_pos as usize];
incr_pos += 1;
}
}
result
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy