HR-IPK Arrays: Left Rotation

1 개요[ | ]

HR-IPK Arrays: Left Rotation
해커랭크 면접준비키트
문제 C C++ C# Go Java node.js Perl PHP Python Ruby
Arrays e
HR-IPK 2D Array - DS
HR-IPK Arrays: Left Rotation
HR-IPK New Year Chaos
HR-IPK Minimum Swaps 2
HR-IPK Array Manipulation

2 Java[ | ]

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

public class Solution {
    // Complete the rotLeft function below.
    static int[] rotLeft(int[] a, int d) {
        d = d % a.length;
        int[] arr = new int[a.length];
        for (int i=d; i<d+a.length; i++) {
            arr[i-d] = a [i % a.length];
        }
        return arr;
    }
    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();
    }
}

3 PHP[ | ]

<?php
function rotLeft($a, $d) {
    return array_merge(array_splice($a,$d), array_splice($a,0,$d));
}
$fptr = fopen(getenv("OUTPUT_PATH"), "w");
$stdin = fopen("php://stdin", "r");
fscanf($stdin, "%[^\n]", $nd_temp);
$nd = explode(' ', $nd_temp);
$n = intval($nd[0]);
$d = intval($nd[1]);
fscanf($stdin, "%[^\n]", $a_temp);
$a = array_map('intval', preg_split('/ /', $a_temp, -1, PREG_SPLIT_NO_EMPTY));
$result = rotLeft($a, $d);
fwrite($fptr, implode(" ", $result) . "\n");
fclose($stdin);
fclose($fptr);
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}