
나의 풀이
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;
}
}
'알고리즘' 카테고리의 다른 글
| [프로그래머스] 하샤드 수(Java) (0) | 2024.11.05 |
|---|---|
| [프로그래머스] 정수 내림차순으로 배치하기(Java) * (0) | 2024.11.04 |
| [프로그래머스] 문자열을 정수로 바꾸기(Java) (0) | 2024.11.02 |
| [프로그래머스] 배열 두 배 만들기 (Java) (0) | 2024.11.01 |
| [프로그래머스] 자연수 뒤집어 배열로 만들기(Java) * (0) | 2024.11.01 |