Einfach 0 gelöst 0% Erfolgsquote

Move Zeroes

Move every zero to the end while keeping the other order.

Das lernst du

  • Move elements with a write pointer
  • Keep the relative order of non-zero values
  • Fill the remaining positions with zeros

Problem

Übersetzung anzeigen

Return the list with all zeros moved to the end. The non-zero values keep their relative order.

Implement move_zeroes(numbers).

Input

numbers: The list of integers.

Output

Return the rearranged list.

Einschränkungen

0 ≤ length ≤ 100,000

Beispiele

Beispiel 1
Eingabe:
numbers = [0, 1, 0, 3, 12]

Erwartete Ausgabe:
expected = [1, 3, 12, 0, 0]
Eingabe:
std::vector<long long> numbers = {0, 1, 0, 3, 12};

Erwartete Ausgabe:
std::vector<long long> expected = {1, 3, 12, 0, 0};
Eingabe:
long[] numbers = new long[] {0, 1, 0, 3, 12};

Erwartete Ausgabe:
long[] expected = new long[] {1, 3, 12, 0, 0};

Erläuterung

1, 3, 12 keep their order and the two zeros go last: [1, 3, 12, 0, 0].

Beispiel 2
Eingabe:
numbers = [0, 0]

Erwartete Ausgabe:
expected = [0, 0]
Eingabe:
std::vector<long long> numbers = {0, 0};

Erwartete Ausgabe:
std::vector<long long> expected = {0, 0};
Eingabe:
long[] numbers = new long[] {0, 0};

Erwartete Ausgabe:
long[] expected = new long[] {0, 0};

Erläuterung

Only zeros: the list stays [0, 0].

So gehst du vor

Versuch es zuerst allein und öffne dann einen Hinweis nach dem anderen.

Hinweis 1

Non-zero values should keep their order while zeros drift to the end.

Hinweis 2

A two-pointer or write-index approach lets you rebuild the list in one pass.

Hinweis 3

Do not reorder the non-zero values when compacting them.

Erwartete Komplexität

O(n) time, O(1) extra space

Referenzen

Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.

Die beste bestandene Lösung jedes Coders. Schnellere, sparsamere und besser bewertete Lösungen stehen weiter oben.

Noch keine bestandene Lösung. Löse sie und hol dir Platz eins.

Von anderen lernen

Community-Lösungen

Noch keine veröffentlichten Lösungen. Löse die Aufgabe und teile als Erster eine Lösung.