"050-implement Pow (x, N) (n-order x)"
"leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index"
Original Question
Implement POW (x, N).
Main Topic
The n-th side of X.
Thinking of solving problems
Recursive solution.
Code Implementation
Algorithm implementation class
Public classSolution { Public Double Mypow(DoubleXintN) {if(x = =0&& n = =0) {Throw NewIllegalArgumentException (); }//Index plus signBoolean isnegative =false;//To find the absolute value of n if(N <0) {n = ñ; Isnegative =true; }Doubleresult = POW (x, n);if(isnegative) {return 1.0/result; }Else{returnResult } } Public Double POW(DoubleXintN) {if(n = =0) {return 1; }Else{Doubleresult = POW (x, N/2);//n is odd if(n%2!=0) {returnX * result * result; }Else{returnResult * result; } } }}
Evaluation Results
Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.
Special Instructions
Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47098373"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Leetcode-Interview algorithm classic-java Implementation" "050-implement POW (x, N) (n-order x)"