首页 > 代码库 > LeetCode : reverse integer

LeetCode : reverse integer

解法:

 1 class Solution { 2 public: 3     int reverse(int x) { 4         int signal = (x>0)? 1:-1;   /* signal记录正负号 */ 5         x=abs(x); 6         int tmp = 0; 7  8         if(x == 0) 9             return x;10 11         while(x)12         {13             tmp = tmp*10+x%10;14             x = x/10;15         }16 17         tmp = tmp*signal;      /* 乘上符号 */18 19         return tmp;20     }21 };
View Code