[LeetCode] Remove 9

660. Remove 9

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
int conversion(int n) {
long res = 0, i = 1;
while(n) {
res = res + (n%9) * i;
n/=9;
i *= 10;
}
return res;
}
public:
int newInteger(int n) {
return conversion(n);
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/14/PS/LeetCode/remove-9/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.