[LeetCode] Finding 3-Digit Even Numbers

아르당·2026년 8월 5일

LeetCode

목록 보기
447/561
post-thumbnail

문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음

Problem

정수 배열 digits이 주어지고, 각 요소는 숫자이다. 배열은 중복을 포함할 수 있다.
주어진 조건을 만족하는 모든 고유한 정수를 찾아라.

  • 정수는 임의의 순서로 digits에서 3개의 요소를 연속해서 구성된다.
  • 정수는 0이 맨 앞에 오지 않는다.
  • 정수는 짝수이다.

예를 들어, digits이 [1, 2, 3]이 주어졌을 때, 정수 132와 312는 조건을 만족한다.
고유한 정수의 배열을 정렬해서 반환해라.

Example

#1
Input: digits = [2, 1, 3, 0]
Output: [102, 120, 130, 132, 210, 230, 302, 310, 312, 320]

#2
Input: digits = [2, 2, 8, 8, 2]
Output: [222, 228, 282, 288, 822, 828, 882]

#3
Input: digits = [3, 7, 5]
Output: []

Constraints

  • 3 <= digits.length <= 100
  • 0 <= digits[i] <= 9

Solved

class Solution {
    public int[] findEvenNumbers(int[] digits) {
        int[] nums = new int[10];

        for(int digit : digits){
            nums[digit]++;
        }

        List<Integer> result = new ArrayList<>();

        for(int i = 1; i <= 9; i++){
            if(nums[i] == 0){
                continue;
            }

            nums[i]--;

            for(int j = 0; j <= 9; j++){
                if(nums[j] == 0){
                    continue;
                }

                nums[j]--;

                for(int k = 0; k <= 8; k += 2){
                    if(nums[k] == 0){
                        continue;
                    }

                    result.add(i * 100 + j * 10 + k);
                }

                nums[j]++;
            }

            nums[i]++;
        }

        return result.stream().mapToInt(Integer::intValue).toArray();
    }
}
profile
내 마음대로 코드 작성하는 세상

0개의 댓글