코린이의 소소한 공부노트

[LeetCode/Easy] 2729. Check if The Number is Fascinating 본문

코딩테스트 풀이/JAVA

[LeetCode/Easy] 2729. Check if The Number is Fascinating

무지맘 2023. 6. 30. 22:54

1. Input

1) int n

- 3자리 자연수이다.

 

2. Output

1) n2*n, 3*n을 이어 붙여 9자리 자연수를 만들었을 때, 매혹적인 수가 된다면 true, 아니면 false를 반환

- 매혹적인 수란 1부터 9까지의 수를 모두 1개씩 갖고 있고, 0이 없는 수를 말한다.

 

3. Constraint

1) 100 <= n <= 999

 

4. Example

Input: n = 192 -> Output: true

설명: 1922*192=384, 3*192=576을 이어 붙여 만든 수 192384576은 조건을 만족하므로 true를 반환한다.

 

5. Code

class Solution {
    public boolean isFascinating(int n) {
        if(n%10==0) return false;       
        int[] d = new int[10];
        for(int i=1 ; i<=3 ; i++){
            int x = n*i;
            while(x>0){
                d[x%10]++;
                x /= 10;
            }
        }
        if(d[0]!=0) return false;
        for(int i=1 ; i<10 ; i++)
            if(d[i]!=1) return false;
        return true;
    }
}

- 100%, 98%