[LeetCode] Convert Integer to the Sum of Two No-Zero Integers

아르당·2026년 4월 20일

LeetCode

목록 보기
272/303
post-thumbnail

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

Problem

0이 없는 정수는 십진수로 나타냈을 때 0이 포함되지 않는 양의 정수이다.
정수 n이 주어졌을 때, 다음과 같은 조건으로 두 개의 정수 [a, b]를 리스트로 반환해라.

  • a와 b는 0이 아닌 정수이다.
  • a + b = n

Example

#1
Input: n = 2
Output: [1, 1]
Explanation: a = 1, b = 1이다.
a, b 둘 다 0이 아닌 정수이고, a + b = 2 = n이다.

#2
Input: n = 11
Output: [2, 9]
Explanation: a = 2, b = 11이다.
a, b 둘 다 0이 아닌 정수이고, a + b = 11이다.

Constraints

  • 2 <= n <= 10^4

Solved

class Solution {
    public int[] getNoZeroIntegers(int n) {
        for(int i = 1; i < n; i++){
            int j = n - i;

            if(!String.valueOf(i).contains("0") && !String.valueOf(j).contains("0")){
                return new int[]{i, j};
            }
        }

        return new int[0];
    }
}
profile
내 마음대로 코드 작성하는 세상

0개의 댓글