7ms Java using String and Long, is it ok to use String?......


  • 0
    G
    public int reverse(int x) {
        if(x == 0 || x == Integer.MIN_VALUE) return 0;
    	while(x % 10 == 0) x /=10;
    	
    	StringBuilder s = new StringBuilder(String.valueOf(Math.abs(x)));
    	if(x < 0) s.append("-");
    	String res = s.reverse().toString();
    	
        Long l = Long.parseLong(res);
    	if( l > Integer.MAX_VALUE || l < Integer.MIN_VALUE) return 0;
    	
    	return Integer.parseInt(res);
    }

Log in to reply
 

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