해커랭크(HackerRank)

Interview Preparation KitArraysArrays: Left Rotation

cepiloth 2018. 7. 4. 17:03
반응형

https://www.hackerrank.com/challenges/ctci-array-left-rotation/problem?h_l=playlist&slugs%5B%5D=interview&slugs%5B%5D=interview-preparation-kit&slugs%5B%5D=arrays


1. 문제

배열의 요소 회전 하는 문제


2. 알고리즘

키워드 - 배열


3. 코드


1
2
3
4
5
6
7
8
9
10
11
12
13
// Complete the rotLeft function below.
vector<int> rotLeft(vector<int> a, int d) {
    
    const int size = a.size();
    
    vector<int> brr(size);
    for(int i=0; i<size; i++) {
        int data = (i + d) % size;
        brr[i] = a[data];
    }
    
    return brr;
}
cs

반응형