Skip to main content
Back to problems
#660
Hard Algorithms

Remove 9

Math
57.4% acceptance
Mar 31, 2026
166
204
Start from integer 1, remove any integer that contains 9 such as 9, 19, 29... Now, you will have a new integer sequence [1, 2, 3, 4, 5, 6, 7, 8, 10, 11, ...]. Given an integer n, return the nth (1-indexed) integer in the new sequence.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn new_integer(n: i32) -> i32 {
    // The sequence without 9 is essentially base-9 representation
    let mut n = n;
    let mut result = 0;
    let mut base = 1;
    while n > 0 {
      result += (n % 9) * base;
      n /= 9;
      base *= 10;
    }
    result
  }
}