[leetcode] 7. Reverse Integer (easy)

原题

水题 唯一注意的点就是数字溢出

class Solution
{
public:
  int reverse(int x)
  {
    long long MAX = ((long long)1 << 31) - 1;
    long long MIN = 0 - (1 << 31);
    long long a = 0;
    while (x)
    {
      a = a * 10 + x % 10;
      x /= 10;
    }
    return a > MAX || a < MIN ? 0 : a;
  }
};
posted @ 2018-09-15 21:06  Ruohua3kou  阅读(94)  评论(0编辑  收藏  举报