Skip to content

Commit 4ebd8e5

Browse files
committed
leetcode
1 parent e6964b3 commit 4ebd8e5

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

ReverseInteger/reverse_integer.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import "math"
4+
5+
func reverse(x int) int {
6+
7+
var neg bool
8+
if x < 0 {
9+
neg = true
10+
x = x * -1
11+
}
12+
13+
revX := getReversed(x)
14+
if neg == true {
15+
return revX * -1
16+
}
17+
return revX
18+
}
19+
20+
func getReversed(x int) int {
21+
var rev, lastDigit int
22+
for x > 0 {
23+
lastDigit = x % 10
24+
x = x / 10
25+
rev = rev*10 + lastDigit
26+
}
27+
if rev > (math.MaxInt32) {
28+
return 0
29+
}
30+
return rev
31+
}

ReverseInteger/reverse_integer.md

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+

ReverseInteger/reverse_integer.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def reverse(self, x: int) -> int:
3+
neg = False
4+
if x < 0:
5+
neg = True
6+
x = abs(x)
7+
8+
revX = getReversed(x)
9+
10+
if neg:
11+
return -revX
12+
return revX
13+
14+
def getReversed(x: int) -> int:
15+
rev = 0
16+
while x > 0:
17+
lastDigit = x % 10
18+
x //= 10
19+
rev = rev * 10 + lastDigit
20+
21+
if rev > 2**31 - 1:
22+
return 0
23+
return rev

0 commit comments

Comments
 (0)