[LeetCode]Reverse Integer

题意:回文数字。

原题来自:https://leetcode.com/problems/reverse-integer/

分析:

根据题目要求,Reverse digits of an integer.我理解为int的范围了,结果,这题很坑。最先wa的时候,我还以为int范围小了,就用long long,结果还是错。后来百度了,都说这坑,因为该题,自己设定范围。

 1 class Solution {
 2 public:
 3     int reverse(int x) {
 4         long ret = 0;
 5         
 6         while (x != 0) {
 7             ret = ret * 10 + x % 10;
 8             x /= 10;
 9         }
10         
11         // 没有这个,你一定wa,坑爹呀
12         if (ret > 2147483647 || ret < -2147483648) {
13             return 0;
14         }
15         
16         return (int)ret;
17     }
18 };
作者:orange1438
出处:http://www.cnblogs.com/orange1438/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/orange1438/p/4584269.html