A smart method,use java Exception!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


  • 0
    Z

    public class Solution {

    public static int reverse(int x) {
        if (x<0){
    
            String s = String.valueOf(x*(-1));
            StringBuffer stringBuffer = new StringBuffer(s);
            try {
                int result = Integer.parseInt(stringBuffer.reverse().toString());
                return result*-1;
            }catch (Exception e){
                return 0;
            }
    
        }else {
            String s = String.valueOf(x);
            StringBuffer stringBuffer = new StringBuffer(s);
            try {
                return Integer.parseInt(stringBuffer.reverse().toString());
            }catch (Exception e){
                return 0;
            }
        }
    

    }

    }


Log in to reply
 

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