DAY 1
0

# Arrays: Left Rotation

### 題目描述

``````[1, 2, 3, 4, 5] -> [2, 3, 4, 5, 1] -> [3, 4, 5, 1, 2]
``````

``````[1, 2, 3, 4, 5] -> [2, 3, 4, 5, 1] -> [3, 4, 5, 1, 2]
-> [4, 5, 1, 2, 3] -> [5, 1, 2, 3, 4]
``````

### 解題概念：

ex: 陣列大小是 5, 要做 4 次 left rotation 那我們可以算出

...

### 程式碼

``````import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
import java.util.stream.IntStream;

public class Solution {

// Complete the rotLeft function below.
static int[] rotLeft(int[] a, int d) {
int size = a.length;
int rs[] = new int[size];
IntStream.range(0, a.length).forEach(i -> {
int newIndex = (i + d) % size;
rs[i] = a[newIndex];
});
return rs;
}

private static final Scanner scanner = new Scanner(System.in);

public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

String[] nd = scanner.nextLine().split(" ");

int n = Integer.parseInt(nd[0]);

int d = Integer.parseInt(nd[1]);

int[] a = new int[n];

String[] aItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

for (int i = 0; i < n; i++) {
int aItem = Integer.parseInt(aItems[i]);
a[i] = aItem;
}

int[] result = rotLeft(a, d);

for (int i = 0; i < result.length; i++) {
bufferedWriter.write(String.valueOf(result[i]));

if (i != result.length - 1) {
bufferedWriter.write(" ");
}
}

bufferedWriter.newLine();

bufferedWriter.close();

scanner.close();
}
}

``````