首页 > 其他 > 详细

[LeetCode]Reverse Integer

时间:2014-07-03 16:36:49      阅读:311      评论:0      收藏:0      [点我收藏+]

题目:给定一个数字,求出反转后的数字

注意的点:10100输出101,-123456=-654321,-123456789=异常溢出(程序退出)

public class Solution {
    public int reverse(int x) {
	    	long result = 0;
	        boolean isNegative = false;
	        if (x < 0) {
	        	x = -x;
	        	isNegative = true;
	        }
	        
	        while (x > 0) {
	        	result = result*10 + x%10;
	        	x /= 10;
	        }
	        if (result < 0x7fffffff) {
	        	// 2^31-1 = 0x7fffffff
	        	if (!isNegative) {
	        		return (int)result;
	        	} else {
	        		return -(int)result;
	        	}
	        } else {
	        	// overflow
	        	System.exit(1);
	        	return 0;
	        }
	    }
}

[LeetCode]Reverse Integer,布布扣,bubuko.com

[LeetCode]Reverse Integer

原文:http://blog.csdn.net/yeweiouyang/article/details/36632717

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