LeetCode-in-Java

3726. 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:

Solution

public class Solution {
    public long removeZeros(long n) {
        StringBuilder x = new StringBuilder();
        String s = Long.toString(n);
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) != '0') {
                x.append(s.charAt(i));
            }
        }
        return Long.parseLong(x.toString());
    }
}