最近做了一道题目:
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123
Output: 321
Example 2:
Input: -123
Output: -321
Example 3:
Input: 120
Output: 21
Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [?231, 231 ? 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
来源:https://leetcode.com/problems/reverse-integer/
先贴上自己丑陋的代码:
1 classSolution {2 public:3 int reverse(intx) {4 bool isNegative=false;5 if(x==0||xpow(2,31)-1)6 return 0;7 if(x<0){8 isNegative=true;9 x=-1*x;10 }11 while(x%10==0)12 x=x/10;13 int ans=0,temp;14 temp=x-(x/10)*10;15 while(x>=1&&ans=1)22 return 0;23 if(isNegative)24 an