|
| 1 | +# 🔥 Reverse Integer 🔥 || Simple Fast and Easy || with Explanation |
| 2 | + |
| 3 | + |
| 4 | +## Code |
| 5 | + |
| 6 | +```go |
| 7 | +func reverse(x int) int { |
| 8 | + |
| 9 | + var neg bool |
| 10 | + if x < 0 { |
| 11 | + neg = true |
| 12 | + x = x * -1 |
| 13 | + } |
| 14 | + |
| 15 | + revX := getReversed(x) |
| 16 | + if neg == true { |
| 17 | + return revX * -1 |
| 18 | + } |
| 19 | + return revX |
| 20 | +} |
| 21 | + |
| 22 | +func getReversed(x int) int { |
| 23 | + var rev, lastDigit int |
| 24 | + for x > 0 { |
| 25 | + lastDigit = x % 10 |
| 26 | + x = x / 10 |
| 27 | + rev = rev*10 + lastDigit |
| 28 | + } |
| 29 | + if rev > (math.MaxInt32) { |
| 30 | + return 0 |
| 31 | + } |
| 32 | + return rev |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +1. **`reverse(x int) int` Function:** |
| 37 | + * This function takes an integer `x` as input and returns the reversed integer. |
| 38 | + * It first checks if `x` is negative. If it is, it sets a boolean variable `neg` to `true` and converts `x` to its absolute value. |
| 39 | + * It then calls the `getReversed(x)` function to reverse the digits of the (now positive) `x`. |
| 40 | + * Finally, it checks the `neg` flag. If the original number was negative, it negates the reversed integer `revX` before returning it. Otherwise, it returns `revX` directly. |
| 41 | + |
| 42 | +2. **`getReversed(x int) int` Function:** |
| 43 | + * This function takes a positive integer `x` as input and returns its reversed value. |
| 44 | + * It initializes two integer variables: `rev` (to store the reversed integer) and `lastDigit`. |
| 45 | + * It uses a `for` loop to iterate through the digits of `x`. |
| 46 | + * Inside the loop: |
| 47 | + * `lastDigit` is extracted using the modulo operator (`% 10`). |
| 48 | + * `x` is divided by 10 (integer division) to remove the last digit. |
| 49 | + * `rev` is updated by multiplying it by 10 and adding `lastDigit`. This effectively shifts the existing digits of `rev` to the left and adds the new digit to the right. |
| 50 | + * It checks for integer overflow by comparing `rev` to `math.MaxInt32`. If `rev` exceeds this maximum value, it returns 0. |
| 51 | + * It returns the reversed integer `rev`. |
| 52 | + |
| 53 | +**Time and Space Complexity** |
| 54 | + |
| 55 | +**Time Complexity:** |
| 56 | + |
| 57 | +* **`getReversed(x int)`:** The `for` loop iterates once for each digit in the input integer `x`. The number of digits in `x` is proportional to log<sub>10</sub>(n), where n is the absolute value of `x`. Therefore, the time complexity of `getReversed` is O(log<sub>10</sub>(n)). |
| 58 | +* **`reverse(x int)`:** This function calls `getReversed` and performs constant-time operations. Thus, the time complexity of `reverse` is also O(log<sub>10</sub>(n)). |
| 59 | + |
| 60 | +**Space Complexity:** |
| 61 | + |
| 62 | +* **`getReversed(x int)`:** The function uses a constant amount of extra space for the variables `rev` and `lastDigit`. Therefore, the space complexity is O(1). |
| 63 | +* **`reverse(x int)`:** This function uses a constant amount of extra space for the variable `neg` and the return value. Therefore, the space complexity is O(1). |
| 64 | +* Overall, the space complexity of the code is O(1). |
| 65 | + |
| 66 | +* |
| 67 | +**Time Complexity:** |
| 68 | + |
| 69 | +* O(log<sub>10</sub>(n)), where n is the absolute value of the input integer. |
| 70 | + |
| 71 | +**Space Complexity:** |
| 72 | + |
| 73 | +* O(1) (constant space). |
| 74 | + |
0 commit comments