#3726

Remove Zeros in Decimal Representation

Easy
MathSimulationString ManipulationFiltering
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(n)
Space
O(1)
O(n)
💡

Intuition

Time O(n)Space O(n)

Using string manipulation directly to filter out zeros is efficient. We can do this in one pass, making it faster.

⚙️

Algorithm

3 steps
  1. 1Step 1: Convert the integer n to a string.
  2. 2Step 2: Use a list comprehension to filter out '0' characters.
  3. 3Step 3: Join the filtered characters and convert back to an integer.
solution.py2 lines
1def remove_zeros(n):
2    return int(''.join(c for c in str(n) if c != '0'))

Complexity note: The complexity is linear due to a single pass through the string and the creation of a new string.

  • 1Removing characters from a string can be done efficiently using built-in functions.
  • 2Understanding string manipulation is crucial for solving similar problems.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.