Einfach 0 gelöst 0% Erfolgsquote

Rotate Array

Rotate a list to the right by some steps.

Das lernst du

  • Shift elements with index arithmetic
  • Reduce large rotations with modulo
  • Build a rotated copy of a list

Problem

Übersetzung anzeigen

Rotate the list to the right by steps positions: the last element moves to the front, steps times.

Implement rotate_right(numbers, steps).

Input

numbers: The list of integers. steps: How many positions to rotate.

Output

Return the rotated list.

Einschränkungen

0 ≤ length ≤ 100,000; 0 ≤ steps ≤ 10⁹

Beispiele

Beispiel 1
Eingabe:
numbers = [1, 2, 3, 4, 5]
steps = 2

Erwartete Ausgabe:
expected = [4, 5, 1, 2, 3]
Eingabe:
std::vector<long long> numbers = {1, 2, 3, 4, 5};
long long steps = 2;

Erwartete Ausgabe:
std::vector<long long> expected = {4, 5, 1, 2, 3};
Eingabe:
long[] numbers = new long[] {1, 2, 3, 4, 5};
long steps = 2;

Erwartete Ausgabe:
long[] expected = new long[] {4, 5, 1, 2, 3};

Erläuterung

Two right rotations: [5, 1, 2, 3, 4], then [4, 5, 1, 2, 3].

Beispiel 2
Eingabe:
numbers = [1, 2, 3]
steps = 3

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

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

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

Erläuterung

Rotating by the length brings the list back: [1, 2, 3].

So gehst du vor

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

Hinweis 1

A rotation by the list length changes nothing, so only the remainder matters.

Hinweis 2

Think of splitting the list near the end and moving that tail to the front.

Hinweis 3

Handle very large step counts by reducing them before rearranging.

Erwartete Komplexität

O(n) time, O(n) 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.