class Solution { public: int reverse(int x) { int ret = 0; int int_max_divide_10 = INT_MAX / 10; int int_max_mod_10 = INT_MAX % 10; int int_min_divide_10 = INT_MIN / 10; int int_min_mod_10 = INT_MIN % 10; while (x != 0){ int remainder = x % 10; if (x > 0 && (ret > int_max_divide_10 || (ret == int_max_divide_10 && remainder > int_max_mod_10)) || x < 0 && (ret < int_min_divide_10 || (ret == int_min_divide_10 && remainder < int_min_mod_10))) { return 0; } ret *= 10; ret += remainder; x /= 10; } return ret; } };
时间: 2024-08-24 22:32:01