[프로그래머스/JAVA] 없는 숫자 더하기

밍주🫧 ·2025년 3월 2일
0
post-thumbnail

문제 원문

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

문제 정리

0~9까지 수 중에 주어진 배열에 없는 숫자들만 더해서 출력하기

hashSet을 사용하여 문제를 풀었다


import java.util.*;

class Solution {
    public int solution(int[] numbers) {
        int answer = 0;

		//숫자 정렬 -> 할필요가없었다 ㅎ...
        Arrays.sort(numbers);

        HashSet<Integer> hSet = new HashSet<>();

		//0~9까지의 hashSet
        for (int i = 0; i < 10; i++) {
            hSet.add(i);
        }

		
		//hashset에 numbers와 같은 숫자가 있으면 지우기 
        for (int num : numbers) {
            hSet.remove(num);
        }

		//남아있는 숫자 다 더해주기 
        for (int num : hSet) {
            answer += num;
        }

        return answer;
    }
}


성공!

0개의 댓글

관련 채용 정보