Remove 9 — LeetCode 660 Python Solution

HardLeetCode PremiumMath
Problem
#660
Reading time
2 min

The problem

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, ...].

Example

Input
n = 9
Output
10

Complexity

MeasureComplexity
TimeO(n) or O(1)
SpaceO(1) auxiliary

Pattern: Math and Number Theory

Find the closed form, the invariant, or the modular identity — and skip the loop entirely. LeetCode 660. Remove 9 is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Math.

The math and number theory guide has the Python template for the pattern and the 485 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 660. Remove 9?
LeetCode 660. Remove 9 is rated Hard on LeetCode.
What topics does LeetCode 660. Remove 9 cover?
LeetCode 660. Remove 9 is tagged Math on LeetCode.
Is LeetCode 660. Remove 9 a premium problem?
Yes. LeetCode 660. Remove 9 is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview