(Python, Java) 프로그래머스 - 정수 제곱근 판별

[문제 링크]

Python 풀이

import math

def solution(n):
    answer = -1
    half = int(math.sqrt(n))
    if half ** 2 == n:
        answer = (half + 1) ** 2

    return answer

Java 풀이

class Solution {
    public long solution(long n) {
        if (Math.pow((int)Math.sqrt(n),2) == n){
            return (long) Math.pow(Math.sqrt(n)+1,2);
        }
        return -1;
    }
}

© 2021. By Backtony