If I implement is using the following approach, why would I get a "exceed time limit".


  • 0
    K
    import java.lang.*;
    

    public class Solution {
    public int reverse(int x) {
    int i=1;
    int nextDigit=0;
    int digit=0;
    int base=1;
    boolean isNegative= x<0?true:false;
    int result=0;
    Stack<Integer> digits = new Stack<Integer>();

        do{
            base = (int)Math.pow(10,i);
            digit = x%base;
            nextDigit = x/base;
            digits.push(Math.abs(digit));
            i++;
        }while(nextDigit!=0);
        
        while(!digits.empty())
        {
            base = (int)Math.pow(10,i);
            result += digits.pop()*base;
            i--;
        }
        if (isNegative)
            result = result*(-1);
        return result;
    }
    

    }


Log in to reply
 

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