2023. 10. 1. 18:20, 알고리즘/프로그래머스
https://school.programmers.co.kr/learn/courses/30/lessons/120583
#include <string>
#include <vector>
using namespace std;
int solution(vector<int> array, int n) {
int answer = 0;
for(auto c: array){
if(c==n) answer++;
}
return answer;
}
위의 풀이가 내가 작성한 풀이.
다른 사람의 풀이를 보니까 <algorithm> 헤더에 있는 count() 라는 함수를 사용하는 풀이가 있어서 첨부해본다.
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int solution(vector<int> array, int n) {
int answer = 0;
answer = count(array.begin(),array.end(),n);
return answer;
}
위의 풀이처럼 다음에 이러한 형태의 문제를 풀때는 count() 함수를 떠올려서 사용해보도록 하자
'알고리즘 > 프로그래머스' 카테고리의 다른 글
| Lv 0. 직사각형 넓이 구하기. (0) | 2023.10.03 |
|---|---|
| Lv 0. 머쓱이보다 키 큰 사람 (0) | 2023.10.01 |
| Lv 0. 잘라서 배열로 저장하기. (0) | 2023.10.01 |
| Lv 0. 7의 개수 (0) | 2023.10.01 |
| Lv 0. 문자열 정렬하기 (0) | 2023.09.29 |
Comments, Trackbacks
