Only one method on C# POW


  • 0
    A
     public double MyPow(double x, int n) {
            double result=1;
          if(x==1)
          {
              return 1;
          }
             if(x==-1 && n%2==0)
          {
              return 1;
          }
             if(x==-1 && n%2!=0)
          {
              return -1;
          }
          if(n>2147483646 || n<-2147483646)
               return 0;
              
            if(n>=0)
            {
            for(int i=0;i<n;i++){
                result=result*x;
            }
            }
            else
            {
                for(int i=0;i<n*(-1);i++){
                result=result*(1/x);
            } 
            }
         
            return result;
        }
    

Log in to reply
 

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