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 <= 1015Problem 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.
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).
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| String Simulation | O(d) | O(d) | Best for clarity and quick implementation using string filtering |
| Mathematical Digit Construction | O(d) | O(1) | When avoiding extra memory or practicing digit manipulation |
3726. Remove Zeros in Decimal Representation (Leetcode Easy) • Programming Live with Larry • 434 views views
Watch 9 more video solutions →Practice Remove Zeros in Decimal Representation with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor