01/29/2022
Question of the week 01.22.2022
https://leetcode.com/favicon-16x16.png leetcode.com https://leetcode.com/problems/powx-n/
LOADING... https://leetcode.com/problems/powx-n/
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next
interview.
class Solution {
public double myPow(double x, int n) {
if (n >= 0) return findPow(x, n);
if (n == -Integer.MIN_VALUE) return 1.0/(findPow(x, -(n 1)) * x);
return 1.0/findPow(x, -n);
}
private double findPow(double x, int n) {
if (n == 0) return 1.0;
double p = myPow(x, n/2);
double s = p * p;
double answer = (n % 2 == 0) ? s : s*x;
return answer;
}
}
Similar next suggested question - Loading... https://leetcode.com/problems/k-th-symbol-in-grammar/
class Solution {
public int kthGrammar(int n, int k) {
return kth0based(n-1, k-1);
}
private int kth0based(int n, int k) {
if (n == 0) return 0;
int origin = kth0based(n-1, k/2);
return (k % 2 == 0) ? origin : 1 - origin;
}
}
Improvement suggestions solicited. Regards.
class Solution { public double myPow(double x, int n) { if (n >= 0) return findPow(x, n); if (n == -Integer.MIN_VALUE) return 1.0/(findPow(x, -(n+1)) * x); return 1.0/findPow(x, -n); } private double findPow(double x, int n) { if (n == 0) return 1.0; double p = myPow(x, n/2); double s = p * p; doubl...