lintcode413 反转整数

反转整数 

 

将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数)。

样例

给定 x = 123,返回 321

给定 x = -123,返回 -321

 

 1 class Solution {
 2 public:
 3     /**
 4      * @param n the integer to be reversed
 5      * @return the reversed integer
 6      */
 7     int reverseInteger(int n) {
 8         long long res = 0;
 9         while (n) {
10             res = 10 * res + n % 10;
11             n /= 10;
12         }
13         return (res < INT_MIN || res > INT_MAX) ? 0 : res;
14     }
15 };

 

posted on 2017-10-11 15:48  狗剩的美丽家园  阅读(160)  评论(0编辑  收藏  举报

导航