Solution


  • 0
    R

    class Solution {
    public:
    int reverse(int x) {

           long int num,count_dig,i,ans,y,z;
    
           num=x;
           count_dig=0;
           ans=0;
    
           while(num!=0)
           {
              num=num/10;
              count_dig++;
           }
    
           num=x;
    
           for(i=1;i<=count_dig;i++)
           {
              y=1;
    
              z=num%10;
    
              num=num/10;
    
              for(int j=1;j<=count_dig-i;j++)
              {
                 y=y*10;
              }
    
              ans=ans+y*z;
           }
    
        if(ans< INT_MIN || ans> INT_MAX)
        {
            return 0;
        }
        else
        {
           return ans;
        }
    }
    

    };


Log in to reply
 

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