Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
IT NachrichtenPeloton has made a foldable (treadmill)(22.09.2026 um 13:00 Uhr)
IT NachrichtenPeloton has made a foldable (treadmill)(22.09.2026 um 13:00 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Moves Zeroes

Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements. Note that you must do this in-place without making a copy of the array. Example 1: Input: nums =…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.



Note that you must do this in-place without making a copy of the array.



Example 1:

Input: nums = [0,1,0,3,12]

Output: [1,3,12,0,0]



Example 2:

Input: nums = [0]

Output: [0]



Constraints:

1 <= nums.length <= 104

-231 <= nums[i] <= 231 - 1



SOLUTIONS:

Move Zeroes (LeetCode 283) – Optimal In-Place Approach

Problem Statement



Given an array nums, move all 0s to the end while maintaining the relative order of non-zero elements.




The operation must be done in-place

No extra array should be used




Key Idea: Two Pointers



This solution uses two pointers:




l tracks the position where the next non-zero element should be placed

r iterates through the array




Whenever a non-zero element is found at index r, it is swapped with the element at index l, and l is incremented.



Code Implementation

Writing




class Solution:
def moveZeroes(self, nums: List[int]) -> None:
l = 0
for r in range(len(nums)):
if nums[r]:
nums[l], nums[r] = nums[r], nums[l]
l += 1
return nums






Example Walkthrough



Input:

nums = [0, 1, 0, 3, 12]



Step-by-step process:



At index 0: value is 0 → skip

At index 1: value is 1 → swap with index 0 → array becomes [1, 0, 0, 3, 12]

At index 2: value is 0 → skip

At index 3: value is 3 → swap with index 1 → array becomes [1, 3, 0, 0, 12]

At index 4: value is 12 → swap with index 2 → array becomes [1, 3, 12, 0, 0]



Final output:



[1, 3, 12, 0, 0]

Why This Works




  1. All non-zero elements are moved forward as they are encountered. Since the traversal is from left to right and elements are placed in the next available position, the relative order of non-zero elements is preserved. Zeroes automatically shift to the remaining positions at the end.

  2. Complexity Analysis
    3.Time Complexity: O(n), since the array is traversed once

  3. Space Complexity: O(1), since no additional space is used

  4. Key Insight




The condition:

if nums[r]:

is equivalent to:

if nums[r] != 0:


Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Moves Zeroes

Thematisch verwandte Begriffe: Moves, Zeroes · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-94493 | A vulnerability was detected in Gigatech PDV5701 1.0.31_240305_112640. T…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick