-
1레벨 : 제일 작은 수 제거하기코딩문제/프로그래머스 2020. 7. 10. 18:32
stl 에 익숙해지기 위해, algorithm, vector를 사용하여 문제를 풀기로 함. 문제설명에 보이듯, 배열 인수가 하나이면, vector의 pop 을 시키고, -1을 push, 그렇지 않은 경우, 벡터를 하나 더 생성하여, 기존벡터(배열)를 복사한 후, 오름차순 정렬을 시키면, 이 복사한 배열의 0번째 값이 최소값이 된다. 이를 find 함수를 통해 위치를 찾은 후, 인자를 지워주면 된다.
#include <string> #include <vector> #include <algorithm> using namespace std; vector<int> solution(vector<int> arr) { vector<int> tmp; vector<int> tmp2; tmp = arr; tmp2 = tmp; if (tmp.size() == 1) { tmp.pop_back(); tmp.push_back(-1); return tmp; } sort(tmp2.begin(), tmp2.end()); vector<int>::iterator iter; iter = find(tmp.begin(), tmp.end(), tmp2[0]); tmp.erase(iter); return tmp; }
'코딩문제 > 프로그래머스' 카테고리의 다른 글
레벨1 : 달리기 경주(C++) (0) 2023.09.14 2레벨 : 124 나라의 숫자 (0) 2020.07.20 2레벨 : 가장 큰 수 (0) 2020.07.18 1레벨 : 자릿수 더하기 (0) 2020.07.15 레벨1 : 이상한 문자 만들기 (0) 2020.07.10