首页 > 代码库 > LeetCode:Reverse Integer

LeetCode:Reverse Integer

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

 

 1 class Solution { 2 public: 3     int reverse(int x){ 4         int num[100],y=0,i,j; 5         if(x<0) 6         { 7             num[0]=1; 8             x=-x; 9         }10         for(i=1;x>0;i++)11         {12             num[i]=x%10;13             x/=10;14         }15         for(j=1;j<i;j++)16         {17             y+=num[i-j]*pow(10,j-1);18         }19         y=(num[0]==1?-y:y);20         return y;21     }22 };