hwooo
프로그래머스 (C/C++) 181891 : 순서 바꾸기 본문
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;
}
'Study > Algorithm' 카테고리의 다른 글
| 프로그래머스 (C/C++) 181862 : 세 개의 구분자 (0) | 2023.07.05 |
|---|---|
| 프로그래머스 (C/C++) 181846 : 두 수의 합 (0) | 2023.07.03 |
| 프로그래머스 (C/C++) 181913 : 문자열 여러 번 뒤집기 (0) | 2023.07.01 |
| 프로그래머스 (C/C++) 181872 : 특정 문자열로 끝나는 가장 긴 부분 문자열 찾기 (0) | 2023.06.30 |
| 프로그래머스 (C/C++) 42628 : 이중우선순위큐 (0) | 2023.06.30 |