MicrosoftOA #60
Reverse an Integer
EasyJudge-ready
Topics (AI-labelled)Bits & Math
Reverse a 32-bit signed integer without converting it to a string. If the reversed result is outside the 32-bit signed integer range, output 0. The rule of returning 0 on overflow was added from the official statement of the same problem.
Input
One integer x, −2³¹≤x≤2³¹−1.
Output
Output the reversed integer; if the result is outside −2³¹ to 2³¹−1, output 0.
Example 1
Input:
123
Output:
321
Example 2
Input:
120
Output:
21
Example 3
Input:
-120
Output:
-21
Example notes
Keep the sign, reverse the digits, and drop leading zeros; return 0 if the reversed result is out of range.
Editorial
Loading solution…