[
math
]
Leetcode 0660 Remove 9
Problem statement
https://leetcode.com/problems/remove-9/
Solution
It can be shown that our answer is just number written in system with base 9
.
There is logical extension of this problem: what if we remove not digit 9
, but some other digit, or even several of them. In this case we can still use the same logic, but we need to use transform in the end. Imagine, we need to remove digit 7
, then we can have [0,1,2,3,4,5,6,7,8] -> [0,1,2,3,4,5,6,8,9]
and it should work (I am not 100 percent sure, but pretty confident).
Complexity
It is O(log n)
for time and space.
Code
class Solution:
def newInteger(self, n):
res = ""
while n > 0:
res = str(n%9) + res
n //= 9
return int(res)