FunctionRacerProblemsRace

Trim the Zeros

BeginnerTwo pointersPython, JavaScript, TypeScript, Java, C++

Return nums without the zeros at its two ends. Zeros in the middle, with other numbers on both sides, stay where they are.

A list that holds nothing but zeros gives an empty list, and so does an empty list.

Example 1

nums [0,0,3,0,5,0]
returns [3,0,5]

Two zeros at the front and one at the back go. The zero between 3 and 5 stays.

Example 2

nums [0,0,0]
returns []

Every item is a zero, so nothing is left.

Example 3

nums [4,1]
returns [4,1]

There are no zeros at the ends, so the list comes back unchanged.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Two pointers problems