https://programmers.co.kr/learn/courses/30/lessons/86051
코딩테스트 연습 - 없는 숫자 더하기
0부터 9까지의 숫자 중 일부가 들어있는 배열 numbers가 매개변수로 주어집니다. numbers에서 찾을 수 없는 0부터 9까지의 숫자를 모두 찾아 더한 수를 return 하도록 solution 함수를 완성해주세요. 제한
programmers.co.kr
#include <string>
#include <vector>
using namespace std;
int solution(vector<int> numbers) {
bool visited[10] = {0};
int answer = -1;
for(int i= 0; i < numbers.size(); i++) {
int num = numbers[i];
if(visited[num] == 0) visited[num] = true;
}
for(int i= 0; i < 10; i++) {
if(visited[i] == 0) answer += i;
}
if(answer != -1) answer +=1;
return answer;
}
'CodingTest Practice > Programmers' 카테고리의 다른 글
프로그래머스 : 타겟넘버(C++, Lv.2, DFS) (0) | 2022.01.29 |
---|---|
프로그래머스 : 숫자 문자열과 영단어 (C++, Lv.1) (0) | 2021.12.30 |
프로그래머스 : 신규 아이디 추천 (C++, Lv.1) (0) | 2021.12.30 |
프로그래머스 : 문자열 다루기 기본 (C++) (0) | 2021.12.30 |
프로그래머스 : 모의고사 (C++, Lv.1) (0) | 2021.12.30 |