leetcode第七题Reverse Integer (java)

Reverse Integer

Reverse digits of an integer.

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

time=272ms accepted


public class Solution {
   public int reverse(int x) {
		long recv=0;
		int mod=0;
		int xabs=Math.abs(x);
		while(xabs>0){
			mod=xabs%10;
			recv=recv*10+mod;
			xabs/=10;
			if(recv>(Math.pow(2, 31)-1))
				return 0;
		}
		if(x<0)
			recv=-recv;
		
		return (int)recv;
	}
}


posted @ 2015-03-17 14:17  懒人部落  阅读(150)  评论(0编辑  收藏  举报