LeetCode Reverse Integer

Easy!

 1 class Solution {
 2 public:
 3     int reverse(int x) {
 4         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6         
 7         int revnum = 0;
 8         int remainder = 0;
 9         int quotient;
10         
11         if(x > 0)    quotient = x;
12         else if(x < 0)    quotient = -x;
13         else return 0;
14         
15         while( quotient != 0 )
16         {
17             remainder = quotient%10;
18             quotient /= 10;
19             revnum *= 10;
20             revnum += remainder;
21         }
22         
23         if(x >= 0)    return revnum;
24         else        return -revnum;
25       
26     }
27 };


 

another one

:( Not support "itoa"......

 1 class Solution {
 2 public:
 3     int reverse(int x) {
 4         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6         
 7         int revnum;
 8         
 9         char numstr[33];
10         itoa(x, numstr, 10);
11        
12         if(num[0]=='-')
13             numstr = numstr+1;   
14         
15         string revstr(numstr);
16         std::reverse(revstr.begin(), revstr.end());
17         
18         revnum = atoi(revstr);
19         if(num[0]=='-')
20             revnum = 0-revnum;
21         
22         return revnum;
23         
24     }
25 };

Make-up:

原文地址:https://www.cnblogs.com/CathyGao/p/3056835.html