Skip to main content
Back to problems
#1256
Medium Algorithms

Encode number

Math String Bit Manipulation
70.4% acceptance
Mar 31, 2026
82
258

No description available.

Solution

Rust
Time O(1)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn encode(num: i32) -> String {
    // encode(num) = binary representation of (num + 1), with leading '1' removed
    if num == 0 { return String::new(); }
    let n = (num + 1) as u32;
    let bits = format!("{:b}", n);
    bits[1..].to_string()
  }
}