码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode]Reverse Integer

时间:2015-06-17 21:27:48      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

题意:回文数字。

原题来自: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/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

 

[LeetCode]Reverse Integer

标签:

原文地址:http://www.cnblogs.com/orange1438/p/4584269.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!