Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
反转int型整数,输出的也是int型的整数
class Solution { public: int reverse(int x) { long long reverseX=0; int isNegative=1; if(x<0){ isNegative=-1; x*=-1; //取正 } while(x){ reverseX=10*reverseX+x%10; x/=10; } reverseX*=isNegative; if(reverseX>INT_MAX)return INT_MAX; else if(reverseX<INT_MIN)return INT_MIN; return (int)reverseX; } };
LeetCode 007 Reverse Integer,布布扣,bubuko.com
原文地址:http://blog.csdn.net/harryhuang1990/article/details/25781769