`
hcx2013
  • 浏览: 83861 次
社区版块
存档分类
最新评论

Reverse Integer

阅读更多

Reverse digits of an integer.

Example1: x = 123, return 321

Example2: x = -123, return -321

 

public class Solution {
    public int reverse(int x) {
        int num = 0;
        for (; x != 0; x/=10) {
        	if (Math.abs(num) > Integer.MAX_VALUE/10) {
        		return 0;
        	}
        	num = num*10 + x%10;
        }
        return num;
    }
}

 

 

 

0
3
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics