Notice
Recent Posts
Recent Comments
Link
«   2025/12   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Archives
Today
Total
관리 메뉴

hwooo

프로그래머스 (C/C++) 181891 : 순서 바꾸기 본문

Study/Algorithm

프로그래머스 (C/C++) 181891 : 순서 바꾸기

hwooo 2023. 7. 2. 18:31

https://school.programmers.co.kr/learn/courses/30/lessons/181891

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr


풀이

n만큼 위치를 바꿔주면 되는 문제로, 풀이를 보니 방법이 다양해 여러 가지 방법으로 풀어봤다.


코드 1 - 반복문 사용

#include <string>
#include <vector>

using namespace std;

vector<int> solution(vector<int> num_list, int n) {
    vector<int> answer;
    for(int i = n; i < num_list.size(); i++) answer.push_back(num_list[i]);
    for(int i = 0; i < n; i++) answer.push_back(num_list[i]);
    return answer;
}

코드 2 - insert 사용

#include <string>
#include <vector>

using namespace std;

vector<int> solution(vector<int> num_list, int n) {
    vector<int> answer(num_list.begin()+n,num_list.end());
    answer.insert(answer.end(), num_list.begin(), num_list.begin()+n);
    return answer;
}

코드 3 - rotate 사용

#include <string>
#include <vector>
#include <algorithm>
using namespace std;

vector<int> solution(vector<int> num_list, int n) {
    vector<int> answer;
    rotate(num_list.begin(), num_list.begin() + n, num_list.end());        
    return num_list;
}