1. Description
Reverse digits of an integer.
2. Example
x = 123, return 321
x = -123, return -321
3. Code
[restabs alignment="osc-tabs-right" responsive="true" icon="true" text="More" seltabcolor="#fdfdfd" seltabheadcolor="#000" tabheadcolor="blue"]
[restab title="C S= O(1)" active="active"]
int reverse(int x) { long long mResult = 0; while (x) { mResult = mResult * 10 + x % 10; if (mResult > INT_MAX || mResult < INT_MIN) { return 0; } x /= 10; } return mResult; }
[/restab]
[restab title="C (TEST)"]
#include#include int reverse(int x) { long long mResult = 0; while (x) { mResult = mResult * 10 + x % 10; if (mResult > INT_MAX || mResult < INT_MIN) { return 0; } x /= 10; } return mResult; } int main() { printf("%d\n", reverse(1534236469)); system("pause"); return 0; }
[/restab]
[/restabs]
Comments | NOTHING