Move zeros to end (stable)
Java
Medium
11 views
Problem Description
Task: move all zeros to the end while keeping the relative order of non-zero elements.
Output Format
In-place update
Constraints
Do it in O(n) time.
Official Solution
static void moveZeros(int[] a){int w=0;for(int x:a) if(x!=0) a[w++]=x;while(w<a.length) a[w++]=0;}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!