📄 문제
정수 배열 arr과 delete_list가 있습니다. arr의 원소 중 delete_list의 원소를 모두 삭제하고 남은 원소들은 기존의 arr에 있던 순서를 유지한 배열을 return 하는 solution 함수를 작성해 주세요.
📝 풀이
#include <string>
#include <vector>
#include <iterator>
using namespace std;
vector<int> solution(vector<int> arr, vector<int> delete_list) {
for (int i = 0; i < delete_list.size(); ++i) {
for (vector<int>::iterator it = arr.begin(); it != arr.end(); ) {
if (*it == delete_list[i]) {
it = arr.erase(it); // 요소가 같다면 제거
} else {
++it; // 요소가 같지 않다면, 다음 요소로 이동
}
}
}
return arr;
}
- iterator을 이용하여 문제를 해결합니다.
'algorithms (C++)' 카테고리의 다른 글
[C++][프로그래머스] OX퀴즈 (1) | 2023.10.22 |
---|---|
[C++][프로그래머스] 정수를 나선형으로 배치하기 (1) | 2023.10.22 |
[C++][프로그래머스] 전국 대회 선발 고사 (0) | 2023.10.22 |
[C++][프로그래머스] x 사이의 개수 (0) | 2023.10.22 |
[C++] 위상정렬(그래프 정렬) ⭐ (0) | 2023.10.20 |