Leetcode 50: Pow(x, n)

Implement pow(xn).

 

Example 1:

Input: 2.00000, 10
Output: 1024.00000

 

Example 2:

Input: 2.10000, 3
Output: 9.26100

 1 public class Solution {
 2     public double MyPow(double x, int n) {
 3         if (n == 0) return 1;
 4         if (n == 1) return x;
 5         if (n == -1) return 1 / x;
 6         
 7         int left = n / 2;
 8         int right = n - left;
 9         
10         if (left == right)
11         {
12             var l = MyPow(x, left);
13             return l * l;
14         }
15         else
16         {
17             return MyPow(x, left) * MyPow(x, right);
18         }
19     }
20 }

 

posted @ 2017-11-09 02:40  逸朵  阅读(146)  评论(0编辑  收藏  举报