1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
class Solution { public: int reverse(int x) { int result = 0; while (x != 0) { int digit = x % 10; x /= 10; if (__builtin_smul_overflow(result, 10, &result)) return 0; if (__builtin_sadd_overflow(result, digit, &result)) return 0; } return result; } };