class Solution {
public double myPow(double x, int n) {
if (n == 0) {
return 1.0;
}
double result = x;
int i = 1;
while(i < Math.abs(n)) {
result *= x;
i++;
}
if (n < 0) result = 1 / result;
return result;
}
}
Time limit exceeded
299 / 304 test cases passed.
반띵하는 방식을 생각해냄 (logn)
class Solution {
public double myPow(double x, int n) {
if (n == 0) {
return 1.0;
}
double result = 1;
int i = Math.abs(n);
double multiple = x;
while(i > 0) {
if (i % 2 == 1) {
result = result * multiple;
}
multiple = multiple * multiple;
i = i / 2;
}
if (n < 0) result = 1 / result;
return result;
}
}
진심 개 거지같은 케이스 하나 통과 안됨..
303 / 304 test cases passed.
Status: Wrong Answer
2.00000
-2147483648
외죠?
class Solution {
public double myPow(double x, int n) {
if (n == 0) {
return 1.0;
}
long N = n;
if (N < 0) {
x = 1 / x;
N = -N;
}
double result = 1;
double multiple = x;
while(N > 0) {
if (N % 2 == 1) {
result = result * multiple;
}
multiple = multiple * multiple;
N = N / 2;
}
return result;
}
}
Runtime: 0 ms, faster than 100.00% of Java online submissions for Pow(x, n).
Memory Usage: 38.5 MB, less than 32.56% of Java online submissions for Pow(x, n).
루션이 나랑 같은거 같은데 걍 long으로만 바꿔줌... 뭐하자는것?