hechenghai

现在已经不走技术路线了,可能没法回答大家的问题了,抱歉!

Reverse Integer

Reverse digits of an integer.

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

click to show spoilers.

Have you thought about this?

Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!

If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.

Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?

For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

class Solution {
    
    	public int reverse(Integer x) {
		
		long result=0;
		long factor = (x>=0)?1:-1;
		long temp = x*factor;
		
		while (temp>0) {
			long a = temp%10;
			result = result*10+a;
			temp=temp/10;			
		}
		
		if(result>Integer.MAX_VALUE){
			return 0;
		}
		return (int)(result*factor);
    }
    
};


阅读更多
个人分类: LeetCode
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

Reverse Integer

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭