Skip to the code
cf.
+25 XP

#657 Mirror a Signed Counter

medium

A retro scoreboard widget can show a counter mirrored: its digits in reverse order, keeping the sign. The scoreboard only stores 32-bit signed integers, from -2147483648 to 2147483647.

Write mirrorCounter(n) that returns n with its digits reversed and the same sign (1230 becomes 321). If the mirrored value doesn't fit in the 32-bit range, return 0.

Example 1

Input: n = 1230
Output: 321

The digits 1230 reversed are 0321, which is 321.

Example 2

Input: n = -45
Output: -54

The digits reverse and the sign stays.

Constraints

  • -2^31 <= n <= 2^31 - 1
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).