HardNo. 0660LeetCode Premium

Remove 9 — LeetCode 660 Python Solution

Topics
Math
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

Time
O(n) or O(1)
Space · auxiliary
O(1)

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

Related problems
No.ProblemDifficulty
0667Beautiful Arrangement IIMedium
0002Add Two NumbersMedium
0007Reverse IntegerMedium
0009Palindrome NumberEasy
0043Multiply StringsMedium
0048Rotate ImageMedium

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.

Free to download. Check it runs on your machine, then pick a plan.