首页 > 其他 > 详细

Leetcode 7 Reverse Integer

时间:2015-02-15 11:58:38      阅读:218      评论:0      收藏:0      [点我收藏+]

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

 

题目解析:

你要是敢用string解你就输了!!

一位一位运算吧骚年, 没啥好解释的, 注意overflow和负数情况就行~

 

 1 public int reverse(int x) {
 2         int res = 0;
 3         while(x != 0){
 4             if(Math.abs(res) > 214748364)
 5                 return 0;
 6             res = res * 10 + x % 10;
 7             x = x / 10;
 8         }
 9         if(x < 0)
10             res = -res;
11         return res;
12 }

 

Leetcode 7 Reverse Integer

原文:http://www.cnblogs.com/sherry900105/p/4292674.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!