Pow using Templates 4ms


  • -3
    O
    class Solution {
    public:
       template<class T>
        inline constexpr T Pow(const T x, unsigned const n)
        {
        return (n == 0) ? 1 : (x * pow(x, n - 1));
        }
          
        double myPow(double x, int n) 
          {
          if ( n >=0)
            return Pow(x, n);
          else n *= -1;
          return 1 / Pow(x,n);
          }
    };

Log in to reply
 

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