189. Rotate Array

LeetCode medium original: C# #array #csharp #leetcode #medium #two-pointers
Le texte du problème est traduit du russe pour la langue sélectionnée. Le code reste inchangé.

Для целочисленного tableauа nums, поreturn tableau вправо на k шагов, где k — неотрицательное number.

Exemple:

Input: nums = [1,2,3,4,5,6,7], k = 3

Output: [5,6,7,1,2,3,4]

Explanation:

rotate 1 steps to the right: [7,1,2,3,4,5,6]

rotate 2 steps to the right: [6,7,1,2,3,4,5]

rotate 3 steps to the right: [5,6,7,1,2,3,4]

C# solution

correspondant/original
public class Solution {
    public void Rotate(int[] nums, int k) {
        int n = nums.Length;
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[(i + k) % n] = nums[i];
        }
        for (int i = 0; i < n; i++) {
            nums[i] = a[i];
        }
    }
}

C++ solution

brouillon automatique, à relire avant soumission
#include <bits/stdc++.h>
using namespace std;

// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
class Solution {
public:
    public void Rotate(vector<int>& nums, int k) {
        int n = nums.size();
        vector<int>& a = new int[n];
        for (int i = 0; i < n; i++) {
            a[(i + k) % n] = nums[i];
        }
        for (int i = 0; i < n; i++) {
            nums[i] = a[i];
        }
    }
}

Java solution

correspondant/original
class Solution {
    public void rotate(int[] nums, int k) {
        int[] a = new int[nums.length];
        for (int i = 0; i < nums.length; i++) {
            a[(i + k) % nums.length] = nums[i];
        }
        for (int i = 0; i < nums.length; i++) {
            nums[i] = a[i];
        }
    }
}

JavaScript solution

correspondant/original
class Solution {
    rotate(nums, k) {
        const n = nums.length;
        const a = new Array(n);
        for (let i = 0; i < n; i++) {
            a[(i + k) % n] = nums[i];
        }
        for (let i = 0; i < n; i++) {
            nums[i] = a[i];
        }
    }
}

Python solution

correspondant/original
class Solution:
    def rotate(self, nums: List[int], k: int) -> None:
        n = len(nums)
        a = [0] * n
        for i in range(n):
            a[(i + k) % n] = nums[i]

        nums[:] = a

Go solution

correspondant/original
package main

func rotate(nums []int, k int) {
    n := len(nums)
    a := make([]int, n)
    for i := 0; i < n; i++ {
        a[(i+k)%n] = nums[i]
    }
    for i := 0; i < n; i++ {
        nums[i] = a[i]
    }
}

Algorithm

1️⃣

Создаем дополнительный tableau, в который будем помещать каждый element исходного tableauа на его новую позицию. element на позиции i в исходном tableauе будет размещен на индексе (i+k) % длина tableauа.

2️⃣

Копируем elementы из нового tableauа в исходный tableau, сохраняя новый порядок elementов.

3️⃣

Заменяем исходный tableau полученным результатом, завершая процесс поворота tableauа.

😎

Vacancies for this task

offres actives with overlapping task tags are affichés.

Toutes les offres
Il n'y a pas encore d'offres actives.