cswork / OA打开练习工作台

MicrosoftOA #60

Reverse an Integer

简单可在线判题

考点(AI 标注)位运算

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.

输入格式

One integer x, −2³¹≤x≤2³¹−1.

输出格式

Output the reversed integer; if the result is outside −2³¹ to 2³¹−1, output 0.

样例 1

输入:

123

输出:

321

样例 2

输入:

120

输出:

21

样例 3

输入:

-120

输出:

-21

样例说明

Keep the sign, reverse the digits, and drop leading zeros; return 0 if the reversed result is out of range.