My Java Solution


  • 0
    O
    public class Solution {
    	public String reverse(String s){
    		StringBuilder sb=new StringBuilder(s);
    		return sb.reverse().toString();
    	}
        public int reverse(int x) {
            String s=Integer.toString(x);
    		try{
    			if(s.charAt(0)=='-'){
    				return Integer.parseInt("-"+reverse(s.substring(1,s.length())));
    			}else{
    				return Integer.parseInt(reverse(s));
    			}
    		}catch(Exception e){
    			return 0;
    		}
        }
    	public static void main(String[]args){
    		Solution s=new Solution();
    		System.out.println(s.reverse(1534236469));
    		System.out.println(s.reverse(-123));
    	}
    }

Log in to reply
 

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