#include <stdio.h> #include <math.h> // 7. Reverse Integer // Difficulty: Easy // Reverse digits of an integer. // Example1: x = 123, return 321 // Example2: x = -123, return -321 // https://leetcode.com/problems/reverse-integer/ int reverse(int x) { // *** METHOD 2 long y = 0; // a trick, may be improved while (x) { y = y * 10 + x % 10; x /= 10; if (y > pow(2,31) - 1 || y < -1 * pow(2,31)) { printf("(2^31) - 1 = %d, -1 * (2^31) = %d, y = %ld ", (int)pow(2,31) - 1, -1 * (int)pow(2,31), y); return 0; // IMPORTANT: consider range of int } } return y; } int main() { int x = 1534236469; printf("x = %d, reversed x = %d ", x, reverse(x)); return 0; }