Algorithm/C++ - 프로그래머스
프로그래머스 - 제일 작은 수 제거하기 C++
ㅇㅇ잉
2021. 2. 14. 03:34
처음에 내림차순으로 정렬해서 뒤에만 뺐다가 return하는 배열이 순서가 바뀌면 안된다는 걸 알고 다시 풀었다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(vector<int> arr) {
vector<int> answer;
int tmp = arr[0];
for(int i=0;i<arr.size();i++){
if(tmp>arr[i]){
tmp=arr[i];
}
}
for(int i=0;i<arr.size();i++){
if(tmp==arr[i]){
continue;
}
answer.push_back(arr[i]);
}
if(answer.empty()) answer.push_back(-1);
return answer;
}
|
cs |
다른분들 코드인데 참고할만한 것 같다!
*min_element()와 find함수... 이건 몰랐다ㅠㅠ
덕분에 새로운 함수를 배웠다,, 역시 함수 종류도 많이 알고있어야 하는 것 같다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(vector<int> arr) {
vector<int> answer = arr;
int nMin = *min_element(arr.begin(), arr.end());
int pos = find(answer.begin(), answer.end(), nMin) - answer.begin();
answer.erase(answer.begin() + pos);
return answer.empty() ? vector<int>(1, -1) : answer;
}
|
cs |