C# AC solution to handle infinity case


  • 0
    N
    public class Solution {
        public double MyPow(double x, int n) {
            if(n==0) return 1;
            if(n<0){
                n=-n;
                x =1/x;
            }
            
            var ret = n%2 == 0? MyPow(x*x, n/2): MyPow(x*x, n/2) * x;
            return double.IsInfinity(ret)? 0:ret;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.