Accepted 42ms java


  • 0
    A
     public int reverse(int x) {
    	 	boolean minus = false;
    	 	if (x < 0) {
    	 		x = x * (-1);
    	 		minus = true;
    	 	}
       		String strToInt = Integer.toString(x);
    	   	String res = "";
    	    for (int i = strToInt.length()-1; i > -1; i--){
    	    		res += strToInt.charAt(i);
    		}
    	    int resInt;
    	    try {
    			resInt = Integer.parseInt(res);
    		}
    	    catch(Exception e){
    			return 0;
    		}
    	    if (minus == true){
    	    	resInt = resInt * (-1);
    	    }
    	    return resInt;
    }

Log in to reply
 

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