Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Build confidence with an intuition-first walkthrough focused on math fundamentals.
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 summary: You are given a positive integer n. Return the integer obtained by removing all zeros from the decimal representation of n.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
1020030
1
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3726: Remove Zeros in Decimal Representation
class Solution {
public long removeZeros(long n) {
long k = 1;
long ans = 0;
while (n > 0) {
long x = n % 10;
if (x > 0) {
ans = k * x + ans;
k *= 10;
}
n /= 10;
}
return ans;
}
}
// Accepted solution for LeetCode #3726: Remove Zeros in Decimal Representation
func removeZeros(n int64) (ans int64) {
k := int64(1)
for n > 0 {
x := n % 10
if x > 0 {
ans = k*x + ans
k *= 10
}
n /= 10
}
return
}
# Accepted solution for LeetCode #3726: Remove Zeros in Decimal Representation
class Solution:
def removeZeros(self, n: int) -> int:
k = 1
ans = 0
while n:
x = n % 10
if x:
ans = k * x + ans
k *= 10
n //= 10
return ans
// Accepted solution for LeetCode #3726: Remove Zeros in Decimal Representation
fn remove_zeros(n: i64) -> i64 {
n.to_string()
.bytes()
.filter(|&c| c != b'0')
.fold(0, |acc, b| acc * 10 + (b - b'0') as i64)
}
fn main() {
let ret = remove_zeros(10203040506070809i64);
println!("ret={ret}");
}
#[test]
fn test() {
assert_eq!(remove_zeros(1020030), 123);
assert_eq!(remove_zeros(1), 1);
}
// Accepted solution for LeetCode #3726: Remove Zeros in Decimal Representation
function removeZeros(n: number): number {
let k = 1;
let ans = 0;
while (n) {
const x = n % 10;
if (x) {
ans = k * x + ans;
k *= 10;
}
n = Math.floor(n / 10);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.