|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "6f0cd801", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Reverse 32-Bit Integer\n", |
| 9 | + "Reverse the digits of a signed 32-bit integer. If the reversed integer overflows (i.e., is outside the range [−2<sup>31</sup>, −2<sup>31</sup> -1]), return 0. Assume the environment only allows you to store integers within the signed 32-bit integer range." |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "markdown", |
| 14 | + "id": "58d6cb87", |
| 15 | + "metadata": {}, |
| 16 | + "source": [ |
| 17 | + "## Intuition\n", |
| 18 | + "\n", |
| 19 | + "The main challenge in this problem lies in correctly handling edge cases, particularly integer overflow and underflow. Before addressing those, let's first focus on how to reverse an integer in a basic scenario.\n", |
| 20 | + "\n", |
| 21 | + "---\n", |
| 22 | + "\n", |
| 23 | + "### Reversing Positive and Negative Numbers\n", |
| 24 | + "\n", |
| 25 | + "To reverse an integer `n`, follow these steps:\n", |
| 26 | + "\n", |
| 27 | + "1. Extract the last digit: \n", |
| 28 | + " `digit = n % 10`\n", |
| 29 | + "\n", |
| 30 | + "2. Remove the last digit from `n`: \n", |
| 31 | + " `n = n // 10`\n", |
| 32 | + "\n", |
| 33 | + "3. Append the digit to the reversed number: \n", |
| 34 | + " `reversed_n = reversed_n * 10 + digit`\n", |
| 35 | + "\n", |
| 36 | + "This logic works for both positive and negative integers.\n", |
| 37 | + "\n", |
| 38 | + "---\n", |
| 39 | + "\n", |
| 40 | + "### Detecting Integer Overflow\n", |
| 41 | + "\n", |
| 42 | + "If the reversed integer exceeds the maximum value representable by a 32-bit signed integer, i.e., \n", |
| 43 | + "`INT_MAX = 2^31 - 1 = 2147483647`, \n", |
| 44 | + "then we must return `0` to indicate an overflow.\n", |
| 45 | + "\n", |
| 46 | + "We **cannot** check for overflow **after** appending the digit, because the multiplication and addition themselves may already cause an overflow. \n", |
| 47 | + "Instead, we perform a **preemptive check** before appending the digit.\n", |
| 48 | + "\n", |
| 49 | + "```python\n", |
| 50 | + "if reversed_n > INT_MAX // 10:\n", |
| 51 | + " return 0\n", |
| 52 | + "```\n", |
| 53 | + "\n", |
| 54 | + "---\n", |
| 55 | + "\n", |
| 56 | + "#### Why this works?\n", |
| 57 | + "This condition checks whether `reversed_n` is already large enough that multiplying it by 10 would push it past `INT_MAX`.\n", |
| 58 | + "- If `reversed_n > INT_MAX // 10`, then `reversed_n * 10` is guaranteed to exceed `INT_MAX`.\n", |
| 59 | + "- Even if `reversed_n == INT_MAX // 10`, appending any digit > 7 would overflow (since `INT_MAX % 10 == 7`), so a stricter check may also compare the `digit`.\n", |
| 60 | + "\n", |
| 61 | + "But for simplicity and early exit, this inequality suffices to catch imminent overflows.\n", |
| 62 | + "\n", |
| 63 | + "---\n", |
| 64 | + "\n", |
| 65 | + "### Detecting Integer Underflow\n", |
| 66 | + "Similarly, for the minimum 32-bit signed integer value: `INT_MIN = -2^31 = -2147483648`.\n", |
| 67 | + "We check before appending the digit:\n", |
| 68 | + "\n", |
| 69 | + "```python\n", |
| 70 | + "if reversed_n < INT_MIN // 10:\n", |
| 71 | + " return 0\n", |
| 72 | + "```\n", |
| 73 | + "\n", |
| 74 | + "This ensures that multiplying `reversed_n` by 10 won't cause it to drop below `INT_MIN`." |
| 75 | + ] |
| 76 | + }, |
| 77 | + { |
| 78 | + "cell_type": "code", |
| 79 | + "execution_count": 1, |
| 80 | + "id": "d08c74c9", |
| 81 | + "metadata": {}, |
| 82 | + "outputs": [], |
| 83 | + "source": [ |
| 84 | + "import math\n", |
| 85 | + "\n", |
| 86 | + "def reverse_32_bit_integer(n: int) -> int:\n", |
| 87 | + " INT_MAX = 2**31 - 1\n", |
| 88 | + " INT_MIN = -2**31\n", |
| 89 | + " reversed_n = 0\n", |
| 90 | + "\n", |
| 91 | + " while n != 0:\n", |
| 92 | + " digit = int(math.fmod(n, 10))\n", |
| 93 | + " n = int(n / 10)\n", |
| 94 | + "\n", |
| 95 | + " if (reversed_n > int(INT_MAX / 10) or reversed_n < int(INT_MIN // 10)):\n", |
| 96 | + " return 0\n", |
| 97 | + "\n", |
| 98 | + " reversed_n = reversed_n * 10 + digit\n", |
| 99 | + " \n", |
| 100 | + " return reversed_n" |
| 101 | + ] |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "markdown", |
| 105 | + "id": "d0f98617", |
| 106 | + "metadata": {}, |
| 107 | + "source": [ |
| 108 | + "### Complexity Analysis\n", |
| 109 | + "The time complexity is O(log(n)) because we loop through each digit of n, of which there are roughly log(n) digits. As this environment supports 32-bit integers, the time complexity can also be considered O(1).\n", |
| 110 | + "\n", |
| 111 | + "The space complexity is O(1)." |
| 112 | + ] |
| 113 | + } |
| 114 | + ], |
| 115 | + "metadata": { |
| 116 | + "kernelspec": { |
| 117 | + "display_name": "Python 3", |
| 118 | + "language": "python", |
| 119 | + "name": "python3" |
| 120 | + }, |
| 121 | + "language_info": { |
| 122 | + "codemirror_mode": { |
| 123 | + "name": "ipython", |
| 124 | + "version": 3 |
| 125 | + }, |
| 126 | + "file_extension": ".py", |
| 127 | + "mimetype": "text/x-python", |
| 128 | + "name": "python", |
| 129 | + "nbconvert_exporter": "python", |
| 130 | + "pygments_lexer": "ipython3", |
| 131 | + "version": "3.11.9" |
| 132 | + } |
| 133 | + }, |
| 134 | + "nbformat": 4, |
| 135 | + "nbformat_minor": 5 |
| 136 | +} |
0 commit comments