GitHubSeob
C++ / 프로그래머스 / 제일 작은 수 제거하기 본문
문제
https://programmers.co.kr/learn/courses/30/lessons/12935
코딩테스트 연습 - 제일 작은 수 제거하기
정수를 저장한 배열, arr 에서 가장 작은 수를 제거한 배열을 리턴하는 함수, solution을 완성해주세요. 단, 리턴하려는 배열이 빈 배열인 경우엔 배열에 -1을 채워 리턴하세요. 예를들어 arr이 [4,3,2,1
programmers.co.kr
문제풀이
코드
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(vector<int> arr) {
vector<int> answer;
int min_num = *min_element(arr.begin(), arr.end());
if (arr.size() == 1) answer.push_back(-1);
else {
bool visit = false;
for (int idx = 0; idx < arr.size(); ++idx)
if(arr[idx]==min_num&&!visit) visit=true;
else answer.push_back(arr[idx]);
}
return answer;
}
'Programmers > Level 1' 카테고리의 다른 글
C++ / 프로그래머스 / 최대공약수와 최소공배수 (0) | 2021.09.13 |
---|---|
C++ / 프로그래머스 / 짝수와 홀수 (0) | 2021.09.13 |
C++ / 프로그래머스 / 정수 제곱근 판별 (0) | 2021.09.13 |
C++ / 프로그래머스 / 정수 내림차순으로 배치하기 (0) | 2021.09.13 |
C++ / 프로그래머스 / 자연수 뒤집어 배열로 만들기 (0) | 2021.09.13 |