題目:Reverse Integergit
Reverse digits of an integer.code
Example1: x = 123, return 321
Example2: x = -123, return -321it
分析:估計是最水的一道題之一了吧。注意正負數便可。這道題也沒有溢出之類的處理。io
public class Solution { public int reverse(int x) { boolean positive = x > 0; x = Math.abs(x); // make it positive int r = 0; while (x > 0) { r = r* 10 + x % 10; x = x/10; } return positive ? r : -r; } }