Solution in Java


  • 0
    K
    class Solution {
        public static int reverse(int x) 
        {
            try
            {
            int p=x;
            int startindex=0;
            int endindex=0,negative=0;
            String output=" ";
            String temp=Integer.toString(p);
            if(temp.substring(0,1).equals("-"))
            {
                startindex=1;
                negative=1;
            }
            for(int i=startindex;i<temp.length();i++)
            {
                if(i==startindex)
                {
                    output=temp.substring(i,i+1);
                }
                else
                {
                    output=temp.substring(i,i+1)+output;
                }
                
                
            }
            if(negative==1)
                output="-"+output;
            int result=Integer.parseInt(output);
            return result;
            }
            catch(Exception e)
            {
                return 0;
            }
        }
        
        public static void main(String[] args) throws IOException
        {
            Scanner sc=new Scanner(System.in);
            int input=sc.nextInt();
            int output=reverse(input);
        }
    }
    

Log in to reply
 

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