Java use try catch


  • 0
    W

    Stupid solution , but it works

    public int reverse(int x) {
        try {
    	int xxx = (x < 0) ? -x : x;
    	String xstr = String.valueOf(xxx);
    	String temp = "";
    	for (int i = xstr.length() - 1; i >= 0; i--) 
            {
    	    temp += xstr.charAt(i);
    	}
    	if (x < 0) {
    	    return (-Integer.parseInt(temp));
    	} else {
    	    return (Integer.parseInt(temp));
    	}
        } catch (NumberFormatException e) {
    	return new Integer(0);
        }
    }
    

Log in to reply
 

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