Casting exp n value to long a right approach (Java) ?


  • 0

    The problem challenges us of dealing with Integer.MIN_VALUE issue.
    Casting the minimal int value to long an easy solution?

    public class Solution {
        public double myPow(double x, int n) {
            double res = 1;
    
            long m = n; 
            if (m < 0) m = -m; 
            
            while (m > 0) {
                if (m % 2 == 1) res = res * x; 
                x *= x; 
                m /= 2; 
            }
            
            return n < 0 ? 1 / res : res; 
        }
    }
    

Log in to reply
 

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