It is so easy to understand(Java 12 lines)


  • 0
    F
    public class Solution {
        public int reverse(int x) {
            String str = new StringBuilder(x+"").reverse().toString();
            if(!Character.isDigit(str.charAt(str.length()-1))){
            	str = str.charAt(str.length()-1)+str.substring(0, str.length()-1);
            }
            long num = Long.parseLong(str);
            if(num > Integer.MAX_VALUE || num < Integer.MIN_VALUE)
            	return 0;
            return Integer.parseInt(str);
        }
    }
    

  • 0
    K

    nice one the overflow is very annoying , but your long value skipped that problem !! thank you!


  • 0
    D
        public int reverse(int x) {
            String str = new StringBuilder(x+"").reverse().toString();
            if(x<0){
            	str = str.charAt(str.length()-1)+str.substring(0, str.length()-1);
            }
           long num = Long.parseLong(str);
           if(num > Integer.MAX_VALUE || num < Integer.MIN_VALUE)
            	return 0;
            return Integer.parseInt(str);
        }
    

Log in to reply
 

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