Reverse Integer

这题主要要考虑越界处理:

负数小于INT_MIN 正数大于INT_MAX

 1 class Solution {
 2 public:
 3     int reverse(int x) {
 4         bool isnegative=false;
 5         if(x>-10 && x<10) return x;
 6         if(x<0)
 7         {isnegative=true;
 8           x=-x;
 9         }
10         string num;
11         char num_i;
12         while(x!=0)
13         {
14            num_i=x%10+'0';
15            num=num+num_i;
16            x=x/10;
17         }
18         long long y=0;
19       for(int i=0;i<num.length();i++)
20        y=y*10+(num[i]-'0');
21        if(isnegative==true) y=-y;
22        if(y<INT_MIN||y>INT_MAX) return 0;
23        return y;
24         
25     }
26 };
原文地址:https://www.cnblogs.com/daocaorenblog/p/5209682.html