首页 > 代码库 > 7. Reverse Integer

7. 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 max =  2147483647;  //int最大值 5         int min = -2147483648; //int最小值 6          7         long long ret = 0; 8         while(x!=0){ 9             int temp = x % 10;10             ret = ret*10 + temp;11             12             //溢出处理13             if(ret > max || ret < min){14                 return 0;15             }16             17             x = x /10;18             19         }20         return (int)ret;21     }22 };

 

7. Reverse Integer