A left rotation operation on an array of size n shifts each of the array’s elements 1 unit to the left. Given an integer, d, rotate the array that many steps left and return the result.
一個大小為 n 的陣列進行左旋轉操作,將陣列中的每個元素向左移動 1 個單位。 給予一個整數 d,旋轉陣列剩餘的步數並回傳結果。
Example 1:
d = 2 arr = [1, 2, 3, 4, 5] After 2 rotations, arr' = [3, 4, 5, 1, 2].
solution:
運用 javascript 的原生語法函式將陣列元素進行指定位置的切割,
最後用 concat 來重新組合兩個陣列。
Code 1: BigO(n)
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
/*
* Complete the 'rotateLeft' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts following parameters:
* 1. INTEGER d
* 2. INTEGER_ARRAY arr
*/
function rotateLeft(d, arr) {
// Write your code here
const array = [...arr]
const fisrtPart = array.slice(0, d)
const secondPart = array.slice(d)
return secondPart.concat(fisrtPart)
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const firstMultipleInput = readLine().replace(/\s+$/g, '').split(' ');
const n = parseInt(firstMultipleInput[0], 10);
const d = parseInt(firstMultipleInput[1], 10);
const arr = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10));
const result = rotateLeft(d, arr);
ws.write(result.join(' ') + '\n');
ws.end();
}
FlowChart:
Example 1
d = 2 array = [1, 2, 3, 4, 5] fisrtPart = array.slice(0, d) //array.slice(0, 2) [1, 2] secondPart = array.slice(d) //array.slice(2) [3, 4, 5] return secondPart.concat(fisrtPart) //[3, 4, 5, 1, 2]