Reverse digits of an 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         bool isPositive = true;
 5         if(x<0){
 6             isPositive = false;
 7             x = -x;
 8         }
 9         int y = 0;
10         while(x>0){
11            int tmp = x %10;
12            x = x / 10;
13            y = 10 * y +tmp;
14         }
15         if(!isPositive)
16             y = -y;
17          return y;
18     }
19 };

 

posted @ 2014-05-21 22:58  Sara Chi  阅读(250)  评论(0编辑  收藏  举报