← Home

283. Move Zeroes

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
283

This problem looks busy at first, but the accepted solution is built around one steady invariant. For 283. Move Zeroes, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are moveZeroes.

Guide

Why?

The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.

  • The code keeps the moving pieces local, so the main idea stays visible.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01/**
02Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.
03
04Example:
05
06Input: [0,1,0,3,12]
07Output: [1,3,12,0,0]
08Note:
09
10You must do this in-place without making a copy of the array.
11Minimize the total number of operations.
12**/
13
14//Runtime: 16 ms, faster than 99.76% of C++ online submissions for Move Zeroes.
15//Memory Usage: 9.5 MB, less than 91.39% of C++ online submissions for Move Zeroes.
16class Solution {
17public:
18    void moveZeroes(vector<int>& nums) {
19        int fast = 0, slow = 0;
20        for(; fast < nums.size(); fast++){
21            if(nums[fast]){
22                nums[slow++] = nums[fast];
23            }
24        }
25        
26        for(; slow < nums.size(); slow++){
27            nums[slow] = 0;
28        }
29    }
30};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.