Python GrundlagenEinfach

Array-Rotation

Detaillierte Anleitung und Python-Implementierung für das Problem „Array-Rotation“.

Problemstellung

Einfach

Schreiben Sie eine Funktion rotate_array(arr, k), die das Array arr um k Positionen nach links dreht und das gedrehte Array zurückgibt. Elemente, die über den Anfang hinausgehen, werden bis zum Ende umbrochen. Wenn Sie beispielsweise [1,2,3,4,5] um 2 nach links drehen, erhalten Sie [3,4,5,1,2].

Einschränkungen
  • 0 <= len(arr) <= 10^5
  • 0 <= k <= 10^6
  • -10^9 <= arr[i] <= 10^9

Beispiele

Example 1
Input
arr = [1, 2, 3, 4, 5], k = 2
Output
[3, 4, 5, 1, 2]
Explanation

Rotate left by 2: first 2 elements [1,2] move to the end.

Example 2
Input
arr = [10, 20, 30, 40], k = 1
Output
[20, 30, 40, 10]
Explanation

Rotate left by 1: 10 moves to the end.

Example 3
Input
arr = [1, 2, 3], k = 3
Output
[1, 2, 3]
Explanation

Rotating by the array length returns the original array.

Need a Hint?
Erwägen Sie die Verwendung von Array-spezifischen Datenstrukturen wie Sets oder Heaps.
Edge Cases to Watch
  • Leere Eingabestrukturen
  • Einzelelementeingaben
  • Große numerische Grenzen

Bereit zur Lösung?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Im Editor öffnen
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Empfohlene Python-Ressourcen

Erweitern Sie Ihr Wissen mit zugehörigen interaktiven Tutorials, Spickzetteln und Codevergleichen.