Solution in Java


  • 0
    S

    Finally able to get the solution accepted based on iterative fixes using custom test inputs.
    '''
    class Solution {
    public int reverse(int x) {
    int temp = 0;
    List<Integer> lb = new ArrayList<Integer>();
    int y = x;
    while(x>0){
    lb.add(x % 10);
    x = x /10;
    }
    if(x<0){
    y = Math.abs(y);
    while(y>0){
    lb.add(y% 10);
    y = y /10;
    }
    }

        StringBuffer sb = new StringBuffer();
        for(int i= 0; i<lb.size();i++){
            sb.append(lb.get(i));
        }
        if(sb.toString()!= null && sb.toString().length()>0){
            double verifyDouble = Double.valueOf(sb.toString());
            if(verifyDouble > Math.pow(2,31)) {
                return temp;
            }else{
                temp = Integer.valueOf(sb.toString());
            }
        }
        if(x<0){
            temp = temp - (temp + temp );
        }
        return temp;
    }
    

    }
    '''


Log in to reply
 

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