알고리즘
[프로그래머스] 정수 제곱근 판별(Java)
muerha
2024. 11. 4. 17:10

나의 풀이
class Solution {
public long solution(long n) {
for(long x = 1; x * x <= n ; x++){
if(n == x * x){
return (x + 1) * (x + 1);
}
}
return -1;
}
}
다른 사람의 풀이 1
class Solution {
public long solution(long n) {
long answer = 0;
for (long i = 1; i <= n; i++) {
if (i * i == n) {
answer = (i + 1) * (i + 1);
break;
}
else answer = -1;
}
return answer;
}
}
다른 사람의 풀이 2
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;
}
}