首页 > 其他 > 详细

LeetCode 7: Reverse Integer

时间:2015-06-02 00:29:45      阅读:139      评论:0      收藏:0      [点我收藏+]

Reverse digits of an integer.

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

代码如下:

class Solution {
public:
    int reverse(int x) {
        if (x == -x)
	{
		return 0;
	}
	if (x < 0)
	{
		return -reverse(-x);
	}
	int result = 0;
	int cutoff = INT_MAX;
	int cutlim = cutoff %10;
	cutoff = cutoff /10;
	while (x)
	{
		int tmp = x%10;
		result = 10 * result ;
		result += tmp;
		x = x/10;
		if (result>cutoff && x)
		{
		    result = 0;
			break;
		}
			
	}
	return result;
    }
};


LeetCode 7: Reverse Integer

原文:http://blog.csdn.net/sunao2002002/article/details/46318373

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