Skip to content

Commit e67e517

Browse files
authored
Create remove-9.py
1 parent 481254f commit e67e517

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

Python/remove-9.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Time: O(logn)
2+
# Space: O(1)
3+
4+
# Start from integer 1, remove any integer that contains 9 such as 9, 19, 29...
5+
#
6+
# So now, you will have a new integer sequence: 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, ...
7+
#
8+
# Given a positive integer n, you need to return the n-th integer after removing.
9+
# Note that 1 will be the first integer.
10+
#
11+
# Example 1:
12+
# Input: 9
13+
# Output: 10
14+
# Hint: n will not exceed 9 x 10^8.
15+
16+
class Solution(object):
17+
def newInteger(self, n):
18+
"""
19+
:type n: int
20+
:rtype: int
21+
"""
22+
result, base = 0, 1
23+
while n > 0:
24+
result += (n%9) * base
25+
n /= 9
26+
base *= 10
27+
return result

0 commit comments

Comments
 (0)