본문 바로가기
Algorithm/Programmers

[Programmers] Lv.0 / 제곱수 판별하기 / Java

by unknownomad 2024. 4. 24.

문제

 

풀이

class Solution {
    public int solution(int n) {
        return isPerfectSquare(n) ? 1 : 2;
    }
    
    private boolean isPerfectSquare(int num) {
        int sqrt = (int) Math.sqrt(num);
        return sqrt * sqrt == num;
    }
}
class Solution {
    public int solution(int n) {
        return (n % Math.sqrt(n) == 0) ? 1 : 2;
    }
}

 

출처

https://school.programmers.co.kr/learn/courses/30/lessons/120909

댓글