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

LeetCode 7: Reverse Integer

时间:2015-06-02 00:32:52      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:leetcode   reverse integer   

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

标签:leetcode   reverse integer   

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

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