水题 唯一注意的点就是数字溢出
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; }};
本文共 323 字,大约阅读时间需要 1 分钟。
水题 唯一注意的点就是数字溢出
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; }};
转载于:https://www.cnblogs.com/ruoh3kou/p/9893451.html