LeetCode – Pow(x, n)
Problem:
Implement pow(x, n).
This is a great example to illustrate how to solve a problem during a technical interview. The first and second solution exceeds time limit; the third and fourth are accepted.
Java Solution
public double myPow(double x, int n){ if(n==0) return 1; if(n<0){ return 1/helper(x, -n); } double v = helper(x, n/2); if(n%2==0){ return v*v; }else{ return v*v*x; } } |
<pre><code> String foo = "bar"; </code></pre>
-
Alik Elzin
-
Nikhil Bagde
-
Bruce
-
Anushree Acharjee
-
hello
-
carllee1991
-
Dmytro Chyzhykov
-
Amir
-
Aphiwad Chhoeun
-
Sébastien Dijols
-
codeaperan
-
Dan
-
holey