Skip to main content

Remove Zeros in Decimal Representation - Solution & Explanation

EasyMathSimulation7 min read
Practice this problem

Problem Statement

You are given a positive integer n.

Return the integer obtained by removing all zeros from the decimal representation of n.

 

Example 1:

Input: n = 1020030

Output: 123

Explanation:

After removing all zeros from 1020030, we get 123.

Example 2:

Input: n = 1

Output: 1

Explanation:

1 has no zero in its decimal representation. Therefore, the answer is 1.

 

Constraints:

  • 1 <= n <= 1015

Approach Overview

Problem Overview: You are given an integer n. The task is simple: remove every 0 digit from its decimal representation and return the resulting number. The relative order of the remaining digits must stay the same.

Approach 1: String Simulation (O(d) time, O(d) space)

Convert the number to a string and iterate through each character. Skip characters equal to '0' and append the rest to a new string or buffer. After the scan, convert the filtered string back to an integer. The key idea is that string traversal makes digit filtering trivial and avoids manual digit manipulation.

This approach is easy to implement and highly readable. Every digit is processed once, giving O(d) time where d is the number of digits. Extra space is O(d) for the temporary string. This solution relies on straightforward simulation of the digit removal process and is usually the fastest way to write correct code in interviews.

Approach 2: Mathematical Digit Construction (O(d) time, O(1) space)

Instead of converting to a string, process the number using arithmetic. Repeatedly extract digits with n % 10, skip the digit if it equals 0, and rebuild the result using positional multiplication. Because digits are extracted from right to left, you either build the number in reverse and flip it later, or track a multiplier to place digits correctly.

This method avoids additional memory and works entirely with integer operations. The loop runs once per digit, so the time complexity remains O(d) and the extra space is O(1). This solution highlights comfort with math operations and manual digit manipulation, which is common in problems involving decimal representation.

Recommended for interviews: Both approaches run in linear time relative to the number of digits. The string-based simulation is typically expected because it is concise and less error-prone. Showing the mathematical version demonstrates deeper understanding of number manipulation and simulation techniques, which interviewers often appreciate for follow-up discussion.

Solution

We start from the lowest digit of n and check each digit one by one. If the digit is not zero, we add it to the result. We also need a variable to keep track of the current digit position in order to correctly construct the final integer.

Specifically, we can use a variable k to represent the current digit position, then check each digit from the lowest to the highest. If the digit is not zero, we multiply it by k and add it to the result, and then multiply k by 10 for the next digit.

In the end, we obtain an integer without any zeros.

The time complexity is O(d), where d is the number of digits in n. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
String SimulationO(d)O(d)Best for clarity and quick implementation using string filtering
Mathematical Digit ConstructionO(d)O(1)When avoiding extra memory or practicing digit manipulation

Video Solution

3726. Remove Zeros in Decimal Representation (Leetcode Easy)Programming Live with Larry441 views views

Watch 8 more video solutions →

Frequently Asked Questions

Is Remove Zeros in Decimal Representation easy or hard?
Remove Zeros in Decimal Representation is generally classified as an Easy problem. The logic involves basic digit processing using either string traversal or simple arithmetic operations, making it approachable for beginners practicing simulation or math-based problems.
Remove Zeros in Decimal Representation Python/Java solution
In Python or Java, the typical solution converts the number to a string, iterates through characters, skips '0', and reconstructs the result. This approach takes O(d) time and is concise in both languages using built-in string operations.
How to solve Remove Zeros in Decimal Representation in O(n)?
Process the digits sequentially and skip zeros. One method converts the number to a string and filters characters. Another uses repeated modulo and division operations (n % 10 and n / 10) to extract digits and rebuild the result. Both approaches scan digits once, giving O(d) time complexity.
What is the best approach for Remove Zeros in Decimal Representation?
The most practical approach is string-based simulation. Convert the integer to a string, iterate through each character, skip '0', and rebuild the number from the remaining digits. This runs in O(d) time where d is the number of digits and is simple to implement in most languages.
Is Remove Zeros in Decimal Representation asked at Google/Amazon/Meta?
Problems involving digit manipulation and simulation frequently appear in interviews at large tech companies. While this exact problem may vary, the underlying techniques—string processing and mathematical digit extraction—are common in coding interviews at companies like Google, Amazon, and Meta.
What data structure is used in Remove Zeros in Decimal Representation?
The problem mainly uses strings or simple integer variables. The string approach stores digits temporarily while filtering out '0'. The mathematical approach uses only integer arithmetic, making it a constant-space solution without additional data structures.
What is the time complexity of Remove Zeros in Decimal Representation?
The optimal solution runs in O(d) time, where d is the number of digits in the integer. Each digit is processed exactly once while filtering out zeros. Space complexity is O(d) for the string approach or O(1) if you rebuild the number using arithmetic operations.

Ready to solve this problem?

Practice Remove Zeros in Decimal Representation with our built-in code editor and test cases.

Practice on FleetCode