1. 문제
문제 설명
임의의 양의 정수 n에 대해, n이 어떤 양의 정수 x의 제곱인지 아닌지 판단하려 합니다. 제한 사항
|
2. 나의 풀이
class Solution {
public long solution(long n) {
long answer = 0;
double imsi = Math.sqrt(n);
double imsi1 = 0;
for(int i = 0; i <= imsi; i = i + 1) {
if(imsi == i) {
imsi1 = i;
break;
}
}
if(imsi1 == 0) {
answer = -1;
}else {
imsi1 = Math.pow(imsi1+1, 2);
answer = (long)imsi1;
}
return answer;
}
}
3. 다른 사람 풀이
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;
}
}
'알고리즘 공부 > 프로그래머스' 카테고리의 다른 글
프로그래머스 코딩테스트 연습 - 연습문제 - 짝수와 홀수 (0) | 2020.10.06 |
---|---|
프로그래머스 코딩테스트 연습 - 연습문제 - 제일 작은 수 제거하기 (0) | 2020.10.06 |
프로그래머스 코딩테스트 연습 - 연습문제 - 정수 내림차순으로 배치하기 (0) | 2020.10.06 |
프로그래머스 코딩테스트 연습 - 연습문제 - 자연수 뒤집어 배열로 만들기 (0) | 2020.10.04 |
프로그래머스 코딩테스트 연습 - 연습문제 - 자릿수 더하기 (0) | 2020.10.04 |