Skip to content

4051. Remove Zeros In Decimal Representation


4051. Remove Zeros in Decimal Representation

Easy


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

Solution

class Solution {
    public long removeZeros(long n) {  
        long temp = n;
        StringBuilder res = new StringBuilder();
        while (temp > 0) {
            long dig = temp % 10;
            if (dig > 0)
                res.append(dig);
            temp /= 10;
        }
        res.reverse(); 
        return Long.parseLong(res.toString());
    }
}

Complexity Analysis

  • Time Complexity: O(?)
  • Space Complexity: O(?)

Explanation

[Add detailed explanation here]