Very very easy to undersand and memorialble solution in java


  • 0
    N
    public class Solution {
    public int reverse(int x) {
        String number = "" + x;
        String sign = "";
        String result = "";
        try
        {
            if(x == 0)
            {
                return x;
            }
            else if(x < 0)
            {
                sign = "" + number.charAt(0);
                number = number.substring(1);
            }
            
            for(int i = number.length()-1; i >= 0; i--)
            {
                result += number.charAt(i);
            }
            
            return Integer.parseInt(sign + result);
            
        }
        catch(NumberFormatException e)
        {
            return 0;
        }
        
    }
    

    }


Log in to reply
 

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