7. 整数反转

image.png

题解

使用 x % 10 取出个位数

执行用时:1 ms, 在所有 Java 提交中击败了100.00%的用户 内存消耗:35 MB, 在所有 Java 提交中击败了99.82%的用户

  1. class Solution {
  2. public int reverse(int x) {
  3. int res = 0;
  4. while (x != 0) {
  5. if (res < Integer.MIN_VALUE / 10 || res > Integer.MAX_VALUE / 10)
  6. return 0;
  7. res = (res * 10) + (x % 10);
  8. x /= 10;
  9. }
  10. return res ;
  11. }
  12. }