📝 24.01.08
🔗 문제 : https://school.programmers.co.kr/learn/courses/30/lessons/120821?language=csharp
문제 설명
정수가 들어 있는 배열
num_list
가 매개변수로 주어집니다.num_list
의 원소의 순서를 거꾸로 뒤집은 배열을return
하도록solution
함수를 완성해주세요.
using System;
public class Solution {
public int[] solution(int[] num_list) {
int[] answer = new int[num_list.Length];
for(int i = 0; i < num_list.Length; i++) {
answer[num_list.Length - i - 1] = num_list[i];
}
return answer;
}
}
Array
클래스의 Reverse()
함수를 이용한 풀이
Array.Reverse(배열)
using System;
public class Solution {
public int[] solution(int[] num_list) {
Array.Reverse(num_list);
return num_list;
}
}
#include <string>
#include <vector>
using namespace std;
vector<int> solution(vector<int> num_list) {
vector<int> answer;
for(int i = num_list.size() - 1; i >= 0; i--) {
answer.push_back(num_list[i]);
}
return answer;
}
algorithm
라이브러리에 reverse
함수를 활용한 풀이
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(vector<int> num_list) {
vector<int> answer;
reverse(num_list.begin(), num_list.end());
return num_list;
}
기본적으로 제공하는 라이브러리나 함수를 잘 몰라서 빠르게 풀이할 수 없었다. reverse
메서드의 경우 오름차순 되어있는 배열을 내림차순으로 바꾼다거나 하는 식으로 게임에서 활용될 여지가 많으므로 암기해야겠다.