题目描述:整数反转

代码实现:Github


Java代码

  1. public class question_07 {
  2. /**
  3. * 不断地弹出x最后的数,然后推入res的尾部
  4. * 时间复杂度 O(log|x|) 空间复杂度 O(1)
  5. * @param x
  6. * @return
  7. */
  8. public int Solution(int x) {
  9. int res = 0;
  10. while (x != 0) {
  11. if (res < Integer.MIN_VALUE/10 || res > Integer.MAX_VALUE/10) {
  12. return 0;
  13. }
  14. int dight = x % 10;
  15. x /= 10;
  16. res = res * 10 + dight;
  17. }
  18. return res;
  19. }
  20. }