Algorithm/프로그래머스

프로그래머스 없는 숫자 더하기

chbong 2022. 8. 9. 23:26
package ch.programmers;

public class Algorithm6 {
    /*
    프로그래머스
    코딩테스트 연습
    월간 코드 챌린지 시즌3
    없는 숫자 더하기
     */
    class Solution {
        public int solution(int[] numbers) {
            int answer = 0;
            int cnt;
            for(int i= 0 ; i < 10; i ++){
                cnt = 0;
                for(int j = 0; j < numbers.length; j ++){
                    if(numbers[j] == i){
                        cnt++;
                    }
                }
                if(cnt == 0){
                    answer += i;
                }

            }


            return answer;
        }
    }
}

 

풀이

0 ~ 9의 숫자가 주어졌을때 없는 숫자를 구하는 것이므로 i를 0부터 비교하여 numbers의 숫자들과 비교

만약 numbers에 값이 없다면 answer에 없는 값을 추가하며 해결