660. Remove 9

Start from integer 1, remove any integer that contains 9 such as 9, 19, 29...

So now, you will have a new integer sequence: 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, ...

Given a positive integer n, you need to return the n-th integer after removing. Note that 1 will be the first integer.

 

Example 1:

Input: n = 9
Output: 10

 

Constraints:

  • 1 <= n <= 8 x 10^8

Rust Solution

struct Solution;

impl Solution {
    fn new_integer(mut n: i32) -> i32 {
        let mut res = 0;
        let mut base = 1;
        while n > 0 {
            res += n % 9 * base;
            n /= 9;
            base *= 10;
        }
        res
    }
}

#[test]
fn test() {
    let n = 9;
    let res = 10;
    assert_eq!(Solution::new_integer(n), res);
}

Having problems with this solution? Click here to submit an issue on github.